0

I am adding multiple managed matadata fields using CSOM.

public static void createItem(){
    // This is not working
    ListItem newListItem = existingList.AddItem(newItemInfo);
    string termguid1 = getTermGuid(ctx, "term1", "termset1");// function will return term guid
    newListItem["ManagedMatadataField1"] = termguid1;
    string termguid2 = getTermGuid(ctx, "term2", "termset2");
    newListItem["ManagedMatadataField2"] = termguid2;
    newListItem.Update();
    clientContext.Load(existingList);
    clientContext.ExecuteQuery();
}

//EDIT: getTermGuid function which returns correct term guid
public static string getTermGuid(ClientContext clientContext, string termvalue, string termSetName)
        {
            TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore termStore = taxonomySession.TermStores.GetByName("Managed Metadata Service Application");
            TermGroup termGroup = termStore.Groups.GetByName("TermGroup1");
            TermSet termSet = termGroup.TermSets.GetByName(termSetName);
            TermCollection termColl = termSet.Terms;
            clientContext.Load(termColl);
            clientContext.ExecuteQuery();

            foreach (Term term in termColl)
            {
                if (term.Name.Trim() == termvalue.Trim())
                {
                    var termGuid = term.Id.ToString();
                    return termGuid;
                }
            }
            return null;
        }

Above code adding only second Managed Matadata field ManagedMatadataField2 and not first.

While same thing when I am assigning static string guid values as shown in following code for both rather than assign string variable as shown in above code, it's working fine.

// This is working
ListItem newListItem = existingList.AddItem(newItemInfo);
newListItem["ManagedMatadataField1"] = "b0000103-386b-4595-8a6d-82c716a10000";
newListItem["ManagedMatadataField2"] = "cabcd103-386b-4595-8a6d-82c716a1400f";

Above code works fine.

Any Solution ?

It's not throwing any error on ExecuteQuery() but adding only last managed matadata field and overrides all fields above that (in code).

  • What does this return? string termguid2 = getTermGuid(); from code it looks like getTermGuid(); is giving the same value to both string object. – Anuja Dec 6 '17 at 10:17
  • getTermGuid() returns same as what I have given static string like "b0000103-386b-4595-8a6d-82c716a10000" – Ronak Patel Dec 6 '17 at 10:55
  • Can you post the code for getTermGuid() and where you are calling newListItem.Update() and any ExecuteQuery()? – Steve Young Dec 21 '17 at 11:59
  • @SteveYoung , I have updated the code. – Ronak Patel Dec 22 '17 at 7:07
  • I think the problem is that getTermGuid is calling ExecuteQuery between existingList.AddItem and newListItem.Update. Can you try getting the termguid1 and termguid2 first before doing the rest of createItem – Steve Young Jan 2 '18 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.