如何针对条款优化此SharePoint查询?

时间:2015-05-15 05:52:51

标签: c# linq sharepoint caml csom

我有一个托管元数据服务,其中包含一个包含术语集和术语的术语组。

我是SharePoint查询的新手,我目前正在执行以下操作:

  1. 获取客户端背景
  2. 设置凭据
  3. 更新客户端上下文缓存
  4. 获取分类会话
  5. 获取定期商店
  6. 获取字词集
  7. 获取条款
  8. 对于上述每个步骤,我在客户端上下文中加载并执行查询。

    1. 是否有更好的方法来加载和执行查询?
    2. 是否有直接的LINQ查询或CAML查询,我可以根据术语集的UID获取我需要的术语?
    3. 代码:

      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查询?

2 个答案:

答案 0 :(得分:4)

指定示例的主要问题是一堆中间请求被提交到服务器,因此主要优化将是:

  • 消除那些中间请求(参见下面的修改示例)
  • 或者由于SharePoint CSOM支持Request Batching 多个客户端 可以使用单个批处理从服务器请求对象 仅请求

由于您的目标是为特定术语集检索术语,因此可以优化示例,如下所示:

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();
相关问题