Create Managed Metadata Site Column using CSOM

Create Managed Metadata Site Column using CSOM programmatically in SharePoint

In this post i describe you to Create Managed Metadata Site Column using CSOM. You have to first create term set to go in central admin. Then bind it with Metadata field. If you need to create lookup type field then follow my post Create Lookup Type Site Column using CSOM. and want to Create Picture Type Site Column then follow my post Create Picture Type Site Column using CSOM

[AdSense-A]

 ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
string schemaTaxonomyField = "<Field Type='TaxonomyFieldType' Name='ProductCategory' StaticName='ProductCategory' 
    DisplayName='ProductCategory' />"
rootWeb.Fields.AddFieldAsXml(schemaTaxonomyField , true, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();

 

Of course, this is not enough; you also have to bind this field to a termset or term in the term store

Guid termStoreId = Guid.Empty;
Guid termSetId = Guid.Empty;
GetTaxonomyFieldInfo(clientContext, out termStoreId, out termSetId);
 
// Retrieve the field as a Taxonomy Field
TaxonomyField taxonomyField = clientContext.CastTo<TaxonomyField>(field);
taxonomyField.SspId = termStoreId;
taxonomyField.TermSetId = termSetId;
taxonomyField.TargetTemplate = String.Empty;
taxonomyField.AnchorId = Guid.Empty;
taxonomyField.Update();
 
clientContext.ExecuteQuery();

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!