1

I would like to know if is that possible to add a Site column of type "Managed Metadata" to a SharePoint 2010 List with c#

Thanks a lot for your help

Regards,

Arioule

1
1

It depends what API is used:

SSOM

CSOM

How to provision Managed Metadata column via CSOM

Assume the following taxonomy field schema:

<Field Type="TaxonomyFieldType" DisplayName="Country"  ShowField="Term1033" Required="FALSE" EnforceUniqueValues="FALSE" Group="Custom Columns" ID="{474eb740-64bc-40bd-a177-7f7cf9fa8ed5}" SourceID="{6dfea1bb-6a93-4d49-a8cd-7e797716a8e5}" StaticName="Country" Name="Country">
  <Default></Default>
  <Customization>
    <ArrayOfProperty>
      <Property>
        <Name>SspId</Name>
        <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b5b6effd-4925-43e4-ad43-a257213dedec</Value>
      </Property>
      <Property>
        <Name>GroupId</Name>
      </Property>
      <Property>
        <Name>TermSetId</Name>
        <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">b95498b8-600b-4b02-a467-8b6f91823658</Value>
      </Property>
      <Property>
        <Name>AnchorId</Name>
        <Value xmlns:q3="http://www.w3.org/2001/XMLSchema" p4:type="q3:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">00000000-0000-0000-0000-000000000000</Value>
      </Property>
      <Property>
        <Name>UserCreated</Name>
        <Value xmlns:q4="http://www.w3.org/2001/XMLSchema" p4:type="q4:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
      </Property>
      <Property>
        <Name>Open</Name>
        <Value xmlns:q5="http://www.w3.org/2001/XMLSchema" p4:type="q5:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
      </Property>
      <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">{708a33d8-ef52-4e61-b8ca-cfd91a0f2dae}</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">false</Value>
      </Property>
      <Property>
        <Name>IsKeyword</Name>
        <Value xmlns:q8="http://www.w3.org/2001/XMLSchema" p4:type="q8:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
      </Property>
      <Property>
        <Name>TargetTemplate</Name>
      </Property>
      <Property>
        <Name>CreateValuesInEditForm</Name>
        <Value xmlns:q9="http://www.w3.org/2001/XMLSchema" p4:type="q9:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
      </Property>
      <Property>
        <Name>FilterAssemblyStrongName</Name>
        <Value xmlns:q10="http://www.w3.org/2001/XMLSchema" p4:type="q10:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Value>
      </Property>
      <Property>
        <Name>FilterClassName</Name>
        <Value xmlns:q11="http://www.w3.org/2001/XMLSchema" p4:type="q11:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy.TaxonomyField</Value>
      </Property>
      <Property>
        <Name>FilterMethodName</Name>
        <Value xmlns:q12="http://www.w3.org/2001/XMLSchema" p4:type="q12:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">GetFilteringHtml</Value>
      </Property>
      <Property>
        <Name>FilterJavascriptProperty</Name>
        <Value xmlns:q13="http://www.w3.org/2001/XMLSchema" p4:type="q13:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">FilteringJavascript</Value>
      </Property>
    </ArrayOfProperty>
  </Customization>
</Field>

Then the following example demonstrates how to provision field using FieldCollection.AddFieldAsXml method:

public static void ProvisionField(ClientContext ctx,string listTitle, string fieldSchema)
{
    var list = ctx.Web.Lists.GetByTitle(listTitle);
    var field = list.Fields.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
    field.Update();
    ctx.Load(field);
    ctx.ExecuteQuery();
}

Usage

using (var ctx = new ClientContext(webUri))
{
    ctx.Credentials = credentials;
    ProvisionField(ctx,listTitle,fieldSchema);    
}

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.