Sharepoint 2013: Programmatically creating Managed Metadata column & mapping it with termset.

Arunraj Venugopal
 
SharePoint Developer
October 15, 2014
 
Rate this article
 
Views
27063

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.

Author Info

Arunraj Venugopal
 
SharePoint Developer
 
Rate this article
 
Works as a SharePoint Developer in a CMM Level 5 Company ...read more
 

Leave a comment