jump to navigation

Update Enterprise Keyword Column OnPremise : TaxKeyword March 10, 2017

Posted by juanpablo1manrique in SharePoint.
trackback

Hola,

Muy buenas,

private static void TestUpdateKeyWords()
 {
 using (SPSite site = new SPSite(urlSite))
 {
 using (SPWeb web = site.OpenWeb("subweb"))
 {
 SPList list = web.Lists.GetList(new Guid("00000-ad33-4d53-91c0-6310ba1baf09"), true);
 SPListItem item = list.GetItemById(228);
 //CheckOut
 item.File.CheckOut();
 //Set Field
 SPField field = list.Fields.GetFieldByInternalName("TaxKeyword");
 var taxKeywordField = (TaxonomyField)field;
 string[] values = { "keyword1","keyword2" };
 var keywords = values.Select(value => EnsureKeyword(taxKeywordField, value)).ToList();
 string strKeyWordsToInsert = GetTermsString(keywords);
 item[taxKeywordField.Id] = strKeyWordsToInsert;
 item.Update();
 //CheckIn
 item.File.CheckIn("Actualizacion datos", SPCheckinType.MajorCheckIn);

}
 }

}

private static Term EnsureKeyword(TaxonomyField taxField, string name)
 {
 int DefaultLanguage = 1033;

TaxonomySession session = new TaxonomySession(taxField.ParentList.ParentWeb.Site);

var termStore = session.TermStores[0];
 var keywords = termStore.KeywordsTermSet.GetAllTerms();
 var result = keywords.Where(k => k.Name == name);

var keyword = result.FirstOrDefault();
 if (keyword != null)
 {
 return keyword;
 }
 keyword = termStore.KeywordsTermSet.CreateTerm(name, DefaultLanguage, Guid.NewGuid());
 return keyword;
 }

private static string GetTermString(Term term)
 {
 return string.Format("-1;#{0}{1}{2}", term.Name, "|", term.Id);
 }

private static string GetTermsString(IEnumerable<Term> terms)
 {
 var termsString = terms.Select(GetTermString).ToList();
 return string.Join(";#", termsString);
 }

 

Para ver la version Online

http://sharepoint.stackexchange.com/questions/108716/setting-value-of-enterprise-keywords-using-csom

 

Saludos,

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: