Adding a Managed Column to a content type via SharePoint Server 2010 OM

Note: This post is based on Beta 2 and is subject to change in future releases.

Following the previous post, here is the snippet which shows how to add a Managed Column (TaxonomyField) to an existing content type using OM:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using Microsoft.SharePoint;
   6:  using Microsoft.SharePoint.Taxonomy;
   8:  namespace ConsoleApplication
   9:  {
  10:      class Program
  11:      {
  12:          static void Main(string[] args)
  13:          {
  14:              using (SPSite sc = new SPSite(“”))
  15:              {
  16:                  Microsoft.SharePoint.Taxonomy.TaxonomySession session = new Microsoft.SharePoint.Taxonomy.TaxonomySession(sc);
  17:                  TermStore termstore = session.TermStores[“Managed Metadata Service”];
  18:                  Group group = termstore.Groups[“Litware Corporate Taxonomy”];
  19:                  TermSet termset = group.TermSets[“Segment”];
  20:                  SPWeb web = sc.RootWeb;
  21:                  SPContentType ct = web.ContentTypes[“Training Course”];
  22:                  TaxonomyField taxField = web.Fields.CreateNewField(“TaxonomyFieldType”, “TaxonomyColumn”) as TaxonomyField;
  23:                  taxField.SspId = termstore.Id;
  24:                  taxField.TermSetId = termset.Id;
  25:                  taxField.AllowMultipleValues = false;
  26:                  taxField.Group = “Litware”;
  27:                  web.Fields.Add(taxField);
  28:                  ct.FieldLinks.Add(new SPFieldLink(web.Fields[“TaxonomyColumn”]));
  29:                  ct.Update(true);
  30:              }
  31:              Console.Read();
  32:          }
  34:      }
  35:  }

Comments (7)

  1. Jeff Dalton says:

    Question:  Why are you using SPSecurity.RunWithElevatedPrivileges in a Console application?

  2. Ali Mazaheri says:

    Hi Jeff, there is no need to use SPSecurity.RunWithElevatedPrivileges, as I originally copied the snippet from my project that had additional steps requiring to have it there :). Thanks for pointing it out, I removed that piece to avoid confusion …

  3. Amy says:


    This is great information, thank you.  Just wondering if you have tried creating a TaxonomyFieldType field declaratively (ie. using CAML definition and provisioning through a feature)?  Not sure if it’s possible, but I’d like to give it a try.  I’ve posted this question on another blog as well (  Do you have any suggestions?

  4. Ali Mazaheri says:

    Hi Amy, you should be able to do this declaratively as well.

  5. Now that is not something I saw coming, you writing code? well done mate!!!

  6. Dabbu says:


    i want to add keywords as a control on my page layout which is a content type,

    Can you please tell me how do i go about it and how to do it from scracth.

  7. senglory says:

    What if I use ct.Fields instead of web.Fields?