0

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;
        }

1 Answer 1

2

Try it as below:

if (field.TypeAsString == "TaxonomyFieldType")
{
    Guid termStoreId =  Guid.Empty;
    Guid termSetId = Guid.Empty;

    GetTaxonomyFieldInfo(srcContext, out termStoreId, out termSetId);  

    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);

    TaxonomyField taxonomyField = srcContext.CastTo<TaxonomyField>(field);
    taxonomyField.SspId = termStoreId;
    taxonomyField.TermSetId = termSetId;
    taxonomyField.TargetTemplate = String.Empty;
    taxonomyField.AnchorId = Guid.Empty;
    taxonomyField.Update();
    srcContext.ExecuteQuery();

}


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;
}
8
  • Cannot find proxy for type with id "{981cbc68-9edc-4f8d-872f-71146fcbb84f}" exception occurs, may be because it is SP2010
    – KumarV
    Commented Nov 22, 2016 at 13:26
  • Yes, you need to enter the name of the term in the 2013 term set. Commented Nov 22, 2016 at 13:41
  • How to enter the name of the term in the 2013 term set using CSOM?
    – KumarV
    Commented Nov 23, 2016 at 8:46
  • In your 2013, environment create your term in a term set. Also in your code, you need to pass the 2013 context in the GetTaxonomyFieldInfo method Commented Nov 23, 2016 at 9:26
  • please help me with the code
    – KumarV
    Commented Nov 23, 2016 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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