Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to add tags into the enterprise keywords column of shared docs library programatically. I have seen this link Set multiple values to Enterprise Keywords TaxonomyField

and as "Fields.Keywords" is not present as per that link, I have used "TaxKeyWord" as shown below..But I am able to store the tag only in the Term Store, they are not getting stored in the listitem of the library...How do I do the storing to the Enterprise Keyword column?

SPList list = web.Lists["Shared Documents"];
 SPListItem item = list.GetItemById(2);// Id of the document
  string keywordString = "tag1,tag2"; //comma seperated tags

 TaxonomyField managedField = list.Fields.GetFieldByInternalName("TaxKeyword") as TaxonomyField;

                        if (managedField != null)
                        {
                            TaxonomySession session = new TaxonomySession(item.Web.Site, false);
                            TermStore termStore = session.TermStores[managedField.SspId];
                            TermSet termSet = termStore.GetTermSet(managedField.TermSetId);

                            string[] keywords = keywordString.Split(',').Select(s => s.Trim()).ToArray();

                            var terms = new List<Term>();

                            foreach (string keyword in keywords)
                            {
                                Term keywordTerm;
                                if (!termSet.Terms.Any(t => t.Name == TaxonomyItem.NormalizeName(keyword)))
                                {
                                    keywordTerm = termSet.CreateTerm(keyword, CultureInfo.CurrentCulture.LCID);
                                    termStore.CommitAll();
                                }
                                else
                                {
                                    keywordTerm = termSet.Terms[keyword];
                                }

                                terms.Add(keywordTerm);
                            }

                            managedField.SetFieldValue(item, terms);

                        }


                    }
share|improve this question
1  
Do you have item.Update() anywhere? –  Per Jakobsen May 1 '12 at 15:09
    
@Per Jakobsen, I have added the modifed snippet with item.Update below, it does not update the column, do check the snippet –  sanju May 1 '12 at 16:34
    
In the snippet you've added item.SystemUpdate(false) before the ` managedField.SetFieldValue(item, terms);`. It should be after. –  Per Jakobsen May 1 '12 at 19:56
add comment

1 Answer

Here is some code that we use to add terms to the Keyword column:

//try to find the tag in the Keywords term store if it does not exist in the "Topics" term set
            if (termCollection == null || termCollection.Count == 0)
            {
                termCollection = session.DefaultKeywordsTermStore.KeywordsTermSet.GetTerms(termLabel, 1033, true, StringMatchOption.ExactMatch, 100, true);
            }

            //if the term does not exist, create the term and add to the validated list
            if (termCollection != null && termCollection.Count > 0)
            {
                //If we have a one term with an exact match, the add it
                if (validatedTermsList.Count == 1)
                {
                    validatedTermsList.Add(termCollection.First());
                }
                else
                {
                    //if we have multiple terms as a result of the search, try find an exact match for the selected term by matching its GUID
                    var exactMathedTerm = termCollection.Where(t => t.Id == termGUID);
                    if (exactMathedTerm != null && exactMathedTerm.Count() > 0)
                    {
                        //If there is a term matching the guid, then add it, otherwise, add the first one from the result set
                        validatedTermsList.Add(exactMathedTerm.First());
                    }
                    else
                    {
                        validatedTermsList.Add(termCollection.First());
                    }
                }
            }
            else //the term does not exist and needs to be created new
            {
                var validatedTerm = session.DefaultKeywordsTermStore.KeywordsTermSet.CreateTerm(ValidateTermLabel(termLabel), 1033);
                validatedTermsList.Add(validatedTerm);
                isNewTermsAdded = true;
            }
        }

        //if any new term was created commit the changes to the term store
        if (isNewTermsAdded)
        {
            session.DefaultKeywordsTermStore.CommitAll();
        }

And here is the code to update our item:

  //this is used to decide whether to update the children's topic tagging information or not
            var IsTopicChanged = (values.ToString() != discussionItem["TaxKeyword"].ToString());

            TaxonomyFieldValueCollection newValues = new TaxonomyFieldValueCollection(String.Empty);
            if (IsTopicChanged)
            {
                var taxonomyFieldValues = GetValidatedTerms(topicTagged).Select((t) => { return t.GetDefaultLabel(1033) + "|" + t.Id.ToString(); });
                newValues.PopulateFromLabelGuidPairs(String.Join(";", taxonomyFieldValues.ToArray()));

                var taxKeywordField = discussionItem.Fields.GetField("TaxKeyword") as TaxonomyField;
                taxKeywordField.SetFieldValue(discussionItem, newValues);
            }

            discussionItem.Update();
share|improve this answer
    
I am trying to add it to the EnterpriseKeyword column..the program already adds to the TermStore\Keyword of the metadata service...I just modified the above code and am manually setting the TaxKeyword column... <code> .... keywordTerm = termSet.Terms[keyword]; item["TaxKeyword"] = "1033;#"+keywordTerm.Name + "|" + keywordTerm.Id.ToString(); terms.Add(keywordTerm); } item.SystemUpdate(false); managedField.SetFieldValue(item, terms); </code> This doesn't work also to insert into the Enterprise Keyword column. –  sanju May 1 '12 at 16:35
    
sorry could not wrap the code in a code block –  sanju May 1 '12 at 16:51
    
this code doesn't insert the keyword to the manage metadata termstore... is there a way I can insert the keyword from there? –  Carls Jr. Aug 10 '12 at 7:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.