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

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.

Step1:

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

 <Field
          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">
     <Customization>
       <ArrayOfProperty>
         <Property>
           <Name>TextField</Name>
           <Value xmlns:q6="http://www.w3.org/2001/XMLSchema"
                  p4:type="q6:string"
                  xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{A7B047A8-8FAD-4C1C-A14A-434ECB84CAD1}</Value>
         </Property>
       </ArrayOfProperty>
     </Customization>
   </Field>
 

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="http://www.w3.org/2001/XMLSchema-instance">{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>

E.g.:

<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;
                             field.Update();
 
                             break;
                         }
                     }
                 }
             }
 
 
 
         }
 

Include the following code in the feature deactivation

 SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                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;
                 field.Update();
             }
 
         }
 

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

blog comments powered by Disqus

SharePoint Pals

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

Training

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 articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

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