Tuesday 23 April 2013

How to update managed metadata field in SharePoint 2010 programatically

Updating a managed metadata field is not as simple as it looks, the update is not straight forward and needs some extra effort to update, when compared to other field types in SharePoint.

Here is the link which gives some extra insight about the managed metadata in SharePoint 2010.

Here is how we update managed metadata field.


using (SPSite _site = new SPSite("<site collection URL>"))
{
         using (SPWeb _web = _site.OpenWeb())
         {
                        SPDocumentLibrary _docLib =  (SPDocumentLibrary)_web.Lists["<ListName>"];
                        TaxonomySession session = new TaxonomySession(_site);
                        TermStore store = session.TermStores["<Managed Metadata Term store Name>"];
                        Microsoft.SharePoint.Taxonomy.Group group = store.Groups["<Term group Name>"];
                        TermSet set = group.TermSets["Term Set Name"];
                        TaxonomyField tagsField = (TaxonomyField)_docLib.Fields["<Managed Metadata Field Name>"];
                        //we are assuming that managed metadata field values are separated with ';' (if managed metadata field accepts multiple values)
                        List<string> fieldValues = "<Managed Metadata Value>".Split(';').ToList();
                        //if managed metadata field accepts multiple value

                        if (tagsField.AllowMultipleValues)
                        {
                                    //SetTaxonomyFieldMultiValue(termSet, taxField, item, fieldValues);
                                    // declare a collection for the terms to capture all those for a multiple value field
                                    var fieldTerms = new List<Term>();
                                    foreach (var value in fieldValues)
                                    {
                                                    // search for the terms we wish to set the field to
                                                    var terms = set.GetTerms(value, true);
                                                   
                                                    // if we have found a term populate the field
                                                    if (terms.Count > 0)
                                                    {
                                                        terms.ToList().ForEach(t => fieldTerms.Add(t));
                                                    }
                                                    // else leave it blank
                                    }
                                    // set the field to the term(s) we have found
                                    tagsField.SetFieldValue(_docSetItem, fieldTerms);
                  }
                  else
                  {
                             //SetTaxonomyFieldValue(termSet, taxField, item, fieldValues.First())
                             // search for the terms we wish to set the field to
                             var terms = set.GetTerms(fieldValues[0], true, StringMatchOption.ExactMatch, 1, false);
                              // if we have found a term populate the field
                              if (terms.Count > 0)
                              {
                                   // set the field to the term(s) we have found
                                    tagsField.SetFieldValue(_docSetItem, terms.First());
         
                             }
                  }

          }
}

No comments:

Post a Comment