2017-05-30 3 views
1

タイトルや自分の作成したフィールドなどの他のフィールドは編集できますが、アップロードするドキュメントにキーワードを追加する方法はわかりません。これはこれまで私が持っていたものです。私が得たエラーは、キーワード列が存在しないことを示しています。 「キーワード」フィールドのタイプはManaged Metadataです。ここで私はHere is a image of where I want to add keywords intoC#でSharePointのキーワードフィールドを編集するにはどうすればよいですか?

アップデートにキーワードを追加する場所のイメージは次のようになります。ここにあるそれはあなたが管理されたメタデータからキーワードを取得する必要がありますので、最初の分類フィールドの

static void AddMetaData(ClientContext ctx, string fName, string kWords) 
    { 
     List list = ctx.Web.Lists.GetByTitle("Documents"); 
     ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery()); 
     ctx.Load(items); // loading all the fields 
     ctx.ExecuteQuery(); 

     TaxonomySession session = TaxonomySession.GetTaxonomySession(ctx); 
     var store = session.GetDefaultKeywordsTermStore(); 
     var terms = store.KeywordsTermSet.GetAllTerms(); 
     ctx.Load(store, i => i.DefaultLanguage); 
     ctx.ExecuteQuery(); 

     var collection = new System.Collections.ObjectModel.Collection<Term>(); 
     var keywords = kWords.Split(';'); 
     foreach (var key in keywords) 
     { 
      var filtered = ctx.LoadQuery(terms.Where(t => t.Name == key)); 
      ctx.ExecuteQuery(); 
      var term = filtered.SingleOrDefault(); 
      if (term != null) 
       collection.Add(term); 
     } 

     foreach (var item in items) 
     { 
      var taxonomyField = ctx.CastTo<TaxonomyField>(list.Fields.GetByInternalNameOrTitle("Keywords")); 
      ctx.Load(taxonomyField); 
      ctx.ExecuteQuery(); 
      taxonomyField.SetFieldValueByCollection(item, collection, store.DefaultLanguage); 
      item.Update(); 
      ctx.ExecuteQuery(); 
     } 
    } 

答えて

1

を働いたかの更新されたコードは、店舗:次に

TaxonomySession session = TaxonomySession.GetTaxonomySession(context); 
var store = session.GetDefaultKeywordsTermStore(); 
var terms = store.KeywordsTermSet.GetAllTerms(); 
context.Load(store, i => i.DefaultLanguage); 
context.ExecuteQuery(); 

var collection = new System.Collections.ObjectModel.Collection<Term>(); 
var keywords = new string[] { "Key1", "Key2" }; 
foreach (var key in keywords) 
{ 
    var filtered = context.LoadQuery(terms.Where(t => t.Name == key)); 
    context.ExecuteQuery(); 
    var term = filtered.SingleOrDefault(); 
    if (term != null) 
     collection.Add(term); 
} 

TaxonomyFieldクラスに含まれている方法を使用してフィールドを設定:

var taxonomyField = context.CastTo<TaxonomyField>(list.Fields.GetByInternalNameOrTitle("Keywords")); 
context.Load(taxonomyField); 
context.ExecuteQuery(); 
taxonomyField.SetFieldValueByCollection(item, collection, store.DefaultLanguage); 
item.Update(); 
context.ExecuteQuery(); 
+0

私が試してみたところ、「 'Contains'メンバーは表現に使用できません」この行の場合 var filtered = context.LoadQuery(terms.Where(t => keywords.Contains(t.Name))); – zlopez

+0

私は答えを更新しました – tinamou

+0

助けてくれてありがとう!私は2日間この問題を抱えています。私はコードを正しく使用しているかどうかわかりませんstore.DefaultLanguage – zlopez

関連する問題