我有一个托管元数据服务,其中包含一个包含术语集和术语的术语组。
我是SharePoint查询的新手,我目前正在执行以下操作:
对于上述每个步骤,我在客户端上下文中加载并执行查询。
代码:
var siteUrl = ConfigHelper.GetValue("SharepointSiteUrl");
var clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new NetworkCredential(ConfigHelper.GetValue("ServiceAccountLogonName"), ConfigHelper.GetValue("ServiceAccountPassword"));
var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
taxonomySession.UpdateCache();
clientContext.Load(taxonomySession, ts => ts.TermStores);
clientContext.ExecuteQuery();
if (taxonomySession.TermStores.Count == 0)
{
throw new InvalidOperationException("The Taxonomy Service is offline or missing");
}
var termStore = taxonomySession.TermStores[1];
clientContext.Load(termStore);
clientContext.ExecuteQuery();
var termSet = termStore.GetTermSet(new Guid("f40eeb54-7c87-409d-96c7-75ceed6bff60"));
clientContext.Load(termSet);
clientContext.ExecuteQuery();
var terms = termSet.GetAllTerms();
clientContext.Load(terms);
clientContext.ExecuteQuery();
foreach (var term in terms)
{
clientContext.Load(term, t => t.Id, t => t.Name);
clientContext.ExecuteQuery();
}
如何针对条款优化此SharePoint查询?
答案 0 :(得分:4)
指定示例的主要问题是一堆中间请求被提交到服务器,因此主要优化将是:
由于您的目标是为特定术语集检索术语仅,因此可以优化示例,如下所示:
var taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
var termSet = termStore.GetTermSet(termSetId);
var terms = termSet.GetAllTerms();
ctx.Load(terms, tcol => tcol.Include(t => t.Id,t => t.Name));
ctx.ExecuteQuery();
一些建议
TermStoreCollection.GetByName
或
通过索引获取TermStore的TermStoreCollection.GetById
方法
因为在后一种情况下TermStoreCollection
必须初始化
第一TaxonomySession.GetDefaultSiteCollectionTermStore method
需要获得默认的Term Store 答案 1 :(得分:2)
这样
var taxonomySession = TaxonomySession.GetTaxonomySession(ctx);
taxonomySession.UpdateCache();
TermStore ts = taxonomySession.TermStores.GetById(termStoreId);
TermSet set = ts.GetTermSet(termSetId);
TermCollection terms = set.GetAllTerms();
ctx.Load(terms, t=>t.IncludeWithDefaultProperties(term=>term.Name, term=>term.Id));
ctx.ExecuteQuery();