Create Managed Metadata column (TaxonomyFieldType) in Visual Studio

In this article, I would like to show you on how to create Managed Metadata column programmatically in Visual Studio.
Below is the list of steps:

  1. Open Visual Studio 2010 > File > New Project > Empty SharePoint Project, enter the Project Name and Location. Then click OK.timer1
  2. In SharePoint Customization Wizard, Enter your local site URL and Select Deploy as sandboxed solution. Then click Finish.timer2
  3. Right click on the project in Solution Explorer, then select Add > New Item > Site Column. Then enter the Site Column Name.
  4. Edit Elements.xml to look like:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
          <Field ID="{a1f36e30-5e09-4357-9db1-f0819183627d}"
              Type="TaxonomyFieldType"
              DisplayName="BusinessUnit"
              ShowField="Term1033"
              Required="TRUE"
              EnforceUniqueValues="FALSE"
              Group="Custom Columns"
              StaticName="BusinessUnit"
              Name="BusinessUnit"/>
          <Field
              ID="{d6594a04-2987-416a-b2ae-e7fd13c48df4}"
              Name="BusinessUnit_1" StaticName="BusinessUnit_1"
              SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
              Type="Note"
              DisplayName="BusinessUnit_1"
              Group="Custom Columns" ShowInViewForms="FALSE" Required="FALSE" Hidden="TRUE" />
    </Elements>
    
  5. Mapped the site column to the Managed Metadata term group and set when Feature Activated.
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPSite site = properties.Feature.Parent as SPSite;
    
                // Map Managed Metadata site columns
                Guid fieldIdBU = new Guid("{A1F36E30-5E09-4357-9DB1-F0819183627D}");
                Guid noteFieldIdBU = new Guid("{d6594a04-2987-416a-b2ae-e7fd13c48df4}");
                MapManagedMetadataField(fieldIdBU, noteFieldIdBU, site, "Managed Metadata Service", "People", "Business Unit");
            }
    
            private void MapManagedMetadataField(Guid fieldId, Guid noteFieldId, SPSite site, string sTermStore, string sTermGroup, string sTermSet)
            {
                if (site.RootWeb.Fields.Contains(fieldId))
                {
                    TaxonomySession session = new TaxonomySession(site);
    
                    if (session.TermStores.Count != 0)
                    {
                        var termStore = session.TermStores[sTermStore];
                        var group = GetByName(termStore.Groups, sTermGroup);
                        var termSet = group.TermSets[sTermSet];
                        TaxonomyField field = site.RootWeb.Fields[fieldId] as TaxonomyField;
    
                        // Set Manage Metadata's Text field to Note field
                        field.TextField = noteFieldId;
    
                        // Connect to MMS
                        field.SspId = termSet.TermStore.Id;
                        field.TermSetId = termSet.Id;
                        field.TargetTemplate = string.Empty;
                        field.AnchorId = Guid.Empty;
                        field.Update();
                    }
                }
            }
    
            private Group GetByName(GroupCollection groupCollection, string name)
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentException("Not a valid group name", "name");
                }
                foreach (var group in groupCollection)
                {
                    if (group.Name == name)
                    {
                        return group;
                    }
                }
                throw new ArgumentOutOfRangeException("name", name, "Could not find the group");
            }
    

Why do I have to add Note field type ? If you don’t add it, you will have an issue as per my previous article.

Advertisements

Tagged: , , , , , , , ,

5 thoughts on “Create Managed Metadata column (TaxonomyFieldType) in Visual Studio

  1. […] Want to see the full step by step solution? you could go to my other article. […]

  2. […] my previous article to provision managed metadata field, in this article I would like to show you how to set managed metadata field value. This might be […]

  3. Meester Unnone 12/18/2014 at 4:30 PM Reply

    In the XML that creates the field initially, there is a property setting ShowField=”Term1033″. What does that do?

    • tjenho 12/23/2014 at 1:56 PM Reply

      The “ShowField” attribute is required by Managed Metadata Service to select the correct term value. In my scenario here, I set it to “Term1033”.

      This is telling SharePoint that I wish to use 1033 (en-us) as the default term for the term store. You may know that you are able to have terms in different languages. Our default locale here is 1033 and that is want we wish to use.

Leave a Reply

Please log in using one of these methods to post your comment:

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: