2

I am building some VS SharePoint solution. I have a feature which provision columns and a content type. One of the columns is a managed metadata field. I would like to set the Termset on this managed metadata column. How to that?

<Field ID="{0AF6E300-E338-440F-90CB-726C11028EE1}"
       Name="MyCompanyDocTypeTaxHTField0"
       DisplayName="MyCompanyDocType_0"
       Type="Note"
       Required="FALSE"
       ShowInViewForms="FALSE"
       Hidden="TRUE"
       CanToggleHidden="TRUE"
       RowOrdinal="0" />

  <Field ID="{F46BBB43-3E95-4AAA-A1AA-47EBC6B7DD0B}"
         Name="MyCompanyDocType"
         DisplayName="Documenttype"
         Type="TaxonomyFieldType"
         ShowField="Term1033"
         Required="TRUE"
         Group="MyCompany"
         >
    <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">{0AF6E300-E338-440F-90CB-726C11028EE1}</Value>
        </Property>
        <Property>
          <Name>IsPathRendered</Name>
          <Value xmlns:q7="http://www.w3.org/2001/XMLSchema" p4:type="q7:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">true</Value>
        </Property>
      </ArrayOfProperty>
    </Customization>
  </Field>
  • Are you referring to add an instance to the list or to the definition of the column as linked to the specified termset? – nilsandrey Oct 21 '15 at 16:08
2

You have to add below properties to map the column to a termset:

  1. SspId- This is the GUID of managed metadata service
  2. TermSetId- GUID of TermSet

Please try below schema:

<Field ID="{0AF6E300-E338-440F-90CB-726C11028EE1}"
  Type="Note"
 DisplayName="MyCompanyDocType_0"
 Name="MyCompanyDocTypeTaxHTField0"
 ShowInViewForms="FALSE"
Required="FALSE"
 Hidden="TRUE"
CanToggleHidden="TRUE" />

<Field ID="{F46BBB43-3E95-4AAA-A1AA-47EBC6B7DD0B}"
       Type="TaxonomyFieldType"
       DisplayName="MyCompanyDocType"
       ShowField="Term1033"
       Required="TRUE"
       Group="MyCompany"
       Name="MyCompanyDocType" >
  <Customization>
    <ArrayOfProperty>
      <Property>
        <Name>SspId</Name>
        <!--ID of the Managed metadata service -->
        <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">2567ccb0-68f2-46f6-8f4f-36b0bb507422</Value>
      </Property>
      <Property>
        <Name>TermSetId</Name>
        <!--ID of the Termset -->
        <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">2cd1c0fb-88af-4066-a4fc-4c9734160eb5</Value>
      </Property>
      <Property>
        <Name>TextField</Name>
        <!--ID of the Note field declared above -->
        <Value xmlns:q6="http://www.w3.org/2001/XMLSchema" p4:type="q6:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{0AF6E300-E338-440F-90CB-726C11028EE1}</Value>
      </Property>
      <Property>
        <Name>IsPathRendered</Name>
        <Value xmlns:q7="http://www.w3.org/2001/XMLSchema" p4:type="q7:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">true</Value>
      </Property>
    </ArrayOfProperty>
  </Customization>
</Field>
0

Create a feature with the columns and contenttype elements files. Create an feature event receiver as follow:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite site = properties.Feature.Parent as SPSite;
                Guid fieldId = new Guid("{BA879CDD-D0A2-491A-AEF0-ED9AC6F83C4C}"); // custom managed metadata column
                if (site.RootWeb.Fields.Contains(fieldId))
                {
                    TaxonomySession session = new TaxonomySession(site);

                    if (session.TermStores.Count != 0)
                    {
                        var termStore = session.DefaultKeywordsTermStore;

                        if (termStore != null)
                        {
                            var group = termStore.Groups["MyCustomTermGroupName"];
                            var termSet = group.TermSets["MyCustomTermSetName"];
                            TaxonomyField field = site.RootWeb.Fields[fieldId] as TaxonomyField;

                            // Connect to MMS 
                            field.SspId = termSet.TermStore.Id;
                            field.TermSetId = termSet.Id;
                            field.TargetTemplate = string.Empty;
                            field.AnchorId = Guid.Empty;
                            field.Update();
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
  • 1
    This is over kill – Xogle Jun 21 '18 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.