Second Life of a Hungarian SharePoint Geek

February 11, 2010

Adding a managed metadata column to a list via SharePoint Server 2010 object model

Filed under: SP 2010, Taxonomies — Tags: , — Peter Holpar @ 16:24

Ali Mazaheri has posted a similar code recently, but since that was for content type, not list fields, and I found there are some minor difference between the two, I’ll post my solution for adding managed metadata type field to lists.

To tell the truth, I have not tried adding this kind of field to content type, but “translated” the code for handling the list fields instead, and extended with some new features.

I should note, that first I’ve tried to update the field either it was just added  or was added previously, but I’ve received a <nativehr>0x80070057</nativehr><nativestack></nativestack> error when tried to update the newly added field. Probably it was because in this case the field had its Id and InternalName properties empty yet.

  1. AddTaxonomyField(SPList list, bool mayExist, String name, bool required, String description,
  2.     bool addToDefaultView, bool noCrawl, String termStoreName, String groupName, String termSetName, String anchorPath,
  3.     bool allowsMultiple, bool enforceUniqueValues)
  4. {
  5.     SPWeb web = list.ParentWeb;
  6.     TaxonomySession session = new TaxonomySession(web.Site);
  7.     TermStore termStore = session.TermStores[termStoreName];
  8.     Group group = termStore.Groups[groupName];
  9.     TermSet termSet = group.TermSets[termSetName];
  10.  
  11.     TaxonomyField field = null;
  12.     bool fieldExists = false;
  13.  
  14.     // if we allow the field to exist ant it really exist
  15.     // then we get the reference for the instance
  16.     // it will throw an exception if the field exists
  17.     // but has a different type
  18.     if ((mayExist) && (list.Fields.ContainsField(name)))
  19.     {
  20.         field = (TaxonomyField)list.Fields[name];
  21.         fieldExists = true;
  22.     }
  23.     // otherwise we create a new instance
  24.     // it will throw an exception if the field exists
  25.     // and we would like to recreate that
  26.     else
  27.     {
  28.         field = (TaxonomyField)list.Fields.CreateNewField("TaxonomyFieldType", name);
  29.     }
  30.  
  31.     field.Required = required;
  32.     field.Description = description;
  33.     field.SspId = termStore.Id;
  34.     field.TermSetId = termSet.Id;
  35.     field.AllowMultipleValues = allowsMultiple;
  36.     field.EnforceUniqueValues = enforceUniqueValues;
  37.     // field must be indexed to enfoce unique values!
  38.     field.Indexed = enforceUniqueValues;
  39.  
  40.     if (!String.IsNullOrEmpty(anchorPath))
  41.     {
  42.         // see GetTermByPath method in my former post
  43.         Term anchor = termSet.GetTermByPath(anchorPath);
  44.         field.AnchorId = anchor.Id;
  45.     }
  46.  
  47.     field.NoCrawl = noCrawl;
  48.  
  49.     if (!fieldExists)
  50.     {
  51.         list.Fields.Add(field);
  52.     }
  53.     else
  54.     {
  55.         field.Update();
  56.     }
  57.  
  58.     if (addToDefaultView)
  59.     {
  60.         SPView view = list.DefaultView;
  61.         // we add the field if it is new or if the view does not contain already it
  62.         if ((!fieldExists) || (!view.ViewFields.Exists(field.InternalName)))
  63.         {
  64.             view.ViewFields.Add(field.Title);
  65.             view.Update();
  66.         }
  67.     }
  68.  
  69.     return field;
  70. }

For the GetTermByPath method I used to get the value of the AnchorId property see my former post. Anchor is a term that refers to the Term that is the starting point of the “navigation” within a TermSet. Users can select only Term that are under the anchor Term.

Advertisements

1 Comment »

  1. Thanks Peter….

    Comment by madhu Kashyap — March 27, 2012 @ 11:15


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: