Tag Archives: TaxonomyFieldType

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.

%d bloggers like this: