Creating Managed Metadata Field Through CSOM

I am having difficulties understanding if it is possible to create a managed metadata field programatically. In the server side OM, you can use the AddNewField method, however this does not exist in the Client object model. Oddly enough, using Web.Fields.Add() accepts a Field object, however there is no clear way to build a TaxonomyField object without the object path.

Does anyone know of a static method that allows one to create a managed metadata field and associate it with a List object?

TIA!

May 30th, 2013 8:18pm

You must use the AddFieldAsXml method when using CSOM. Below is an example that adds a lookup field. The requirement here is that you need to know the xml schema to use for a Metadata field. You could probably use and existing metadata field as and example.

public static void AddLookupField()
{
            string lookupSchema = @"<Field Type='Lookup' DisplayName='mylookup2' 
                                    Required='FALSE' EnforceUniqueValues='FALSE'
                                    List='{70d6098c-6ba0-4e9e-b101-a60b88fc226a}' 
                                    ShowField='Title' UnlimitedLengthInDocumentLibrary='FALSE' 
                                    RelationshipDeleteBehavior='None' 
                                    StaticName='mylookup' Name='mylookup2'/>";

            ClientContext clientContext = new ClientContext("http://basesmc2008");
            List list = clientContext.Web.Lists.GetByTitle("tester");
            FieldCollection fields = list.Fields;

            clientContext.Load(list);
            clientContext.Load(fields);

            clientContext.ExecuteQuery();

            Field lookupField = fields.AddFieldAsXml(lookupSchema, true, AddFieldOptions.AddToDefaultContentType);
            lookupField.Update();

            clientContext.Load(lookupField);
            clientContext.ExecuteQuery();


}

Free Windows Admin Tool Kit Click here and download it now
May 30th, 2013 9:23pm

Hi,

I was having the same query.I Tried with your solution however may be I am unable to provide ryt xml for that.

here what should be    ID and   SourceID  , new GUID will work ?

I tried with this for  Metadata field but it only creates field but with unselected TermSet  (as no properties defined here) :

 string metadataSchema = @"<Field Type='TaxonomyFieldTypeMulti' DisplayName='MTags' 
                                    Required='FALSE' EnforceUniqueValues='FALSE'
                                    List='{E8ABCDD1-4524-494E-84DD-93A33FBC54A4}' 
                                    WebID='{1b41bcae-56d6-48f0-99d1-ca1a441f21be}'
                                    Mult='TRUE'
                                    ShowField='Title' UnlimitedLengthInDocumentLibrary='FALSE'
                                    ShowInDisplayForm='TRUE'
                                    ShowInEditForm='TRUE'
                                    ShowInViewForms='TRUE' 
                                    Group='TermSetName'
                                    ID='{01D01094-E0C1-40FA-BF0E-296C12EEEC44}'
                                    SourceID='{1b41bcae-56d6-48f0-99d1-ca1a441f21be}' 
                                    StaticName='myTags' Name='MTags'/>";

If we see any existing Metadata field XMlSchema, in addition to this...it will be like below xml file

Here I am unable to figure out how many properties will be there and what will be their values ??
how to make correct field schema for creating managed metadata column ??

<Field Type="TaxonomyFieldTypeMulti" DisplayName="Mytags" List="{27293da6-648b-41f6-afc8-fc856dfc3f19}" WebId="1b41bcae-56d6-48f0-99d1-ca1a441f21be" ShowField="Term1033" Required="FALSE" EnforceUniqueValues="FALSE" Mult="TRUE" Sortable="FALSE" ID="{241efc59-8c8d-4646-b756-25a3b043623a}" SourceID="{ceadce92-09b7-4023-a7c0-9d64708fd8d1}" StaticName="Mytags" Name="Mytags" ColName="int2" RowOrdinal="0" Version="1">
  <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">ef0f8fdc-10bc-4da8-a937-52bcf0261eaa</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">8d4359eb-9a5c-4ad8-bba5-f23cabb5c31a</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">{2b5b7987-9a4f-4112-b883-f57c314e7e0c}</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=14.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>

It would be of great help if anyone can provide me with the solution/suggesstion.

Thanks & Regards,

Soumyap

July 10th, 2013 6:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics