SharePoint Pals
 | Sign In
Sharepoint 2013: Programmatically creating Managed Metadata column & mapping it with termset.
by Arunraj Venugopal 15 Oct 2014
SharePoint Developer
Today  :   5     Total  :    17722

Let us discuss the creation of a Managed Metadata column in a site, adding the managed metadata column in a site content type and mapping the metadata column with the termset in Sharepoint 2013.


Creating managed metadata column by using field element in visual studio.

          Type="TaxonomyFieldTypeMulti"   DisplayName="Tag" StaticName="AceTag"   Name="WCMAceTag"  Group="ACE Custom Columns" ShowField="Term1033"
          Description="Tagging metadata site column for tagging article content category."  Required="FALSE"  EnforceUniqueValues="FALSE" ID="{7B311B12-295F-4923-8D7E-A0B4CBB0E050}"
          DisplaceOnUpgrade="TRUE" Overwrite="TRUE" Mult="TRUE">
           <Value xmlns:q6=""

Major Attributes Information:

Mult: The mult attribute should be true if the type of the managed metadata column is multi values

ShowField: It should Term1033

ID: it should be created using Tools->Create Guid which is different from the id of xmlns:p4="">{A7B047A8-8FAD-4C1C-A14A-434ECB84CAD1}</Value>

Step2: Adding a column in a content type.

Step2.1: Right click on the project and select option “Add” to create a content type file.

Step2.2 Select Content type and provide a name for it.

Step2.3 Add the above column in the <FieldRef> </FieldRef>


<FieldRef DisplayName="Tag" Name="WCMAceTag" Description="Tagging metadata site column for tagging article content category." Required="FALSE" ID="{7B311B12-295F-4923-8D7E-A0B4CBB0E050}" />

Step3: Adding content type in a list which will be created using visual studio’s wizard.

Step3.1: Create a project in visual studio

Step3.2: Select a valid site collection url

Step3.3: Select an option “Sandbox or Farm”

Step3.4: Right click on the project and click add option to create a list

Step3.5: Select a list option in dialog box and provide a name.

Step3.6: Double click on the newly added list instance and click content type.

Step3.7: Select a content type which is newly added in the content type.

Note: Close a solution once it is saved and open the solution to add the content type if you don’t find it at the first time. Check the content is deployed in the site collection properly.

Step4: Add an event receiver in the content type feature by right click on the feature and click Add to include the event receiver.

Include the following code in the feature activation.

 SPSecurity.RunWithElevatedPrivileges (delegate()
                 using (SPSite CurrentSite = properties.Feature.Parent as SPSite)
                     Guid fieldDepartment = new Guid("{78E7263A-6860-4705-ACE5-DEB5B07BFC21}");
                     AssociateMetadata(CurrentSite, fieldDepartment, "Content", "Department");
                     Guid fieldTag = new Guid("{7B311B12-295F-4923-8D7E-A0B4CBB0E050}");
                     AssociateMetadata(CurrentSite, fieldTag, "NET", "StateMap");

Parameter Details:

fieldDepartment is required to have a guid which is for notes field associated with metadata column declaration.

Content and Net are the group name of the term store management.

Statemap and Department are the termset for which the managed metadata column will be mapped.

AssociateMetadata column is a method which will use to map the column using the above parameters

    private void AssociateMetadata(SPSite CurrentSite, Guid fieldId, string _MMSGroupName, string _MMSTermsetName)
             string _MMSServiceAppName = " "ManagedMetadataServiceApplication";
             if (CurrentSite.RootWeb.Fields.Contains(fieldId))
                 TaxonomySession session = new TaxonomySession(CurrentSite);
                 if (session.TermStores.Count != 0)
                     var termStore = session.TermStores[_MMSServiceAppName];
                     foreach (Group grp in termStore.Groups)
                         if (grp.Name.ToUpper() == _MMSGroupName.ToUpper())
                             var group = grp;
                             var termSet = group.TermSets[_MMSTermsetName];
                             TaxonomyField field = CurrentSite.RootWeb.Fields[fieldId] as TaxonomyField;
                             field.SspId = termSet.TermStore.Id;
                             field.TermSetId = termSet.Id;
                             field.TargetTemplate = string.Empty;
                             field.AnchorId = Guid.Empty;

Include the following code in the feature deactivation

                using (SPSite CurrentSite = properties.Feature.Parent as SPSite)
                    Guid fieldDepartment = new Guid("{78E7263A-6860-4705-ACE5-DEB5B07BFC21}");//new Guid("{78E7263A-6860-4705-ACE5-DEB5B07BFC21}");
                    DeAssociateMetadata(CurrentSite, fieldDepartment);
                    Guid fieldTag = new Guid("{7B311B12-295F-4923-8D7E-A0B4CBB0E050}");
                    DeAssociateMetadata(CurrentSite, fieldTag);


DeAssociateMetadata is method that will remove the mapping between managed metadata column and ther termset.

 private void DeAssociateMetadata(SPSite currSite, Guid fieldId)
             if (currSite.RootWeb.Fields.Contains(fieldId))
                 TaxonomyField field = currSite.RootWeb.Fields[fieldId] as TaxonomyField;
                 field.SspId = Guid.Empty;
                 field.TermSetId = Guid.Empty;
                 field.TargetTemplate = string.Empty;
                 field.AnchorId = Guid.Empty;

Note: The example is based on the site collection level feature.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation