2014-12-04 22 views
0

我想在CSOM中设置Sharepoint 2013 TaxonomyField的值,但是当我执行最后的context.ExecuteQuery()时,在taxField.Update()之后抛出异常读取'Microsoft.SharePoint.Client.Runtime.dll中发生类型'Microsoft.SharePoint.Client.ServerException'的第一次机会异常其他信息:值不能为空。参数名称:termId'。一切都很好,它找到了这个词,我没有想法。当通过CSOM添加分类标准值异常“值不能为null。ParamterName:termId”抛出

 public void AddTaxonomyFieldValue(ClientContext context, ListItem item, string destinationColumn, IEnumerable<string> terms, Guid termGroupId, Guid termSetId, int lcid) 
    { 
     var taxonomySession = TaxonomySession.GetTaxonomySession(context); 
     var store = taxonomySession.TermStores.GetByName("Managed Metadata Service2"); 
     var group = store.GetGroup(termGroupId); 
     var set = group.TermSets.GetById(termSetId); 
     var existingTerms = set.GetAllTerms(); 
     var field = item.ParentList.Fields.GetByInternalNameOrTitle(destinationColumn); 
     var taxField = context.CastTo<TaxonomyField>(field); 
     context.Load(existingTerms, et => et.Include(t => t.Labels.Include(l => l.Value, l => l.Language), t => t.Id, t=> t.PathOfTerm, t => t)); 
     context.ExecuteQuery(); 

     var fieldTermValues = new Collection<Term>(); 

     foreach (var term in terms) 
     { 
      var lmi = new LabelMatchInformation(context); 
      lmi.TermLabel = term; 
      // Find existing term 
      var existingTerm = 
       existingTerms.FirstOrDefault(t => t.Labels.Any(l => l.Language == lcid && l.Value == term)); 

      if (null == existingTerm) 
      { 
       // create new term 
       existingTerm = set.CreateTerm(term, lcid, new Guid()); 
       set.TermStore.CommitAll(); 
       context.Load(existingTerm); 
       context.ExecuteQuery(); 
      } 

      fieldTermValues.Add(existingTerm); 
     } 
     // set taxonomy field 
     taxField.SetFieldValueByCollection(item, fieldTermValues, lcid); 
     taxField.Update(); 
     // Exception Thrown Here 
     context.ExecuteQuery(); 
    } 

编辑1: 我只注意到我在foreach循环创建的分类项具有唯一标识符==“00000000-0000-0000-0000-000000000000”它从SharePoint看时现场。所以我在想我的问题在那里。

回答

0

我想出了我的问题,这是由于创建了一个错误的无效术语ID。

在下面的代码

// crete new term 
existingTerm = set.CreateTerm(term, lcid, new Guid()); 

我应该做

existingTerm = set.CreateTerm(term, lcid, Guid.NewGuid()); 
相关问题