I have a list in SP2010, it has a managed metadata column, I need to migrate list to SP2013 and also managed metadata column should be created. I have referenced
[https://karinebosch.wordpress.com/my-articles/creating-fields-using-csom/][1]
[https://blog.mastykarz.nl/programmatically-creating-site-columns-content-types-app-model/][1]
I have used the below code,it does not show any error but also the column is not getting created.
if (field.TypeAsString == "TaxonomyFieldType")
{
Guid termStoreId = Guid.Empty;
Guid termSetId = Guid.Empty;
GetTaxonomyFieldInfo(srcContext, out termStoreId, out termSetId);
TaxonomyField taxonomyField = srcContext.CastTo<TaxonomyField>(field);
taxonomyField.SspId = termStoreId;
taxonomyField.TermSetId = termSetId;
taxonomyField.TargetTemplate = String.Empty;
taxonomyField.AnchorId = Guid.Empty;
taxonomyField.Update();
srcContext.ExecuteQuery();
string fieldSchema = "<Field Type='" + field.FieldTypeKind + "' DisplayName='" + field.Title + "' Name='" + field.InternalName + "' Hidden='" + field.Hidden + "'/>";
targetcollField.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
targetContext.Load(targetcollField);
srcFieldCollections.Add(field);
}
private void GetTaxonomyFieldInfo(ClientContext clientContext, out Guid termStoreId, out Guid termSetId)
{
termStoreId = Guid.Empty;
termSetId = Guid.Empty;
TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext);
TermStore termStore = session.GetDefaultSiteCollectionTermStore();
TermSetCollection termSets = termStore.GetTermSetsByName("ABC", 1033);
clientContext.Load(termSets, tsc => tsc.Include(ts => ts.Id));
clientContext.Load(termStore, ts => ts.Id);
clientContext.ExecuteQuery();
termStoreId = termStore.Id;
termSetId = termSets.FirstOrDefault().Id;
}