CSOM code to convert my drop-down field's choices into terms

john john 931 Reputation points
2020-10-02T11:55:29.05+00:00

I have a field of type Choice with these options:-

Open
In-progress
Approved
Rejected
Cancelled
On-Hold
..
..
..
Closed

and i want to convert this site column into a managed metadata column. so i created a new site column of type managed metadata and i link it to a new term set which contain the above choices as terms. but i am not sure how i can write a CSOM code to do the conversion from the choices to terms, mainly the following:-

1- Get the term's GUID based on the term name (choice name).

2- Assign the GUID to the managed metadata field.

any advice on this please?

thanks

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,573 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,798 questions
{count} vote

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2020-10-05T02:08:20.597+00:00

    Hi @john john ,

    Here is a csom code to set managed metadata field Term value based on Choice field value:

       using (var clientContext = new ClientContext("https://zheguo.sharepoint.com/"))  
                {  
                    clientContext.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);  
                    Web web = clientContext.Web;  
                    clientContext.Load(web);  
                    clientContext.ExecuteQuery();  
                    List list = clientContext.Web.Lists.GetByTitle("MyList");  
                    ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());  
                    clientContext.Load(items);  
                    clientContext.ExecuteQuery();  
                    foreach (ListItem item in items)  
                    {  
                        TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);  
                        TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore();  
                        clientContext.Load(taxonomySession);  
                        clientContext.Load(termStore);  
                        clientContext.ExecuteQuery();  
                        TermGroup termGroup = termStore.Groups.GetByName("Site Collection - zheguo.sharepoint.com");  
                        clientContext.Load(termGroup);  
                        TermSet termSet = termGroup.TermSets.GetByName("TestTermSet");  
                        clientContext.Load(termSet);  
                        Term term = termSet.Terms.GetByName(item["TestChoice"].ToString());  
                        clientContext.Load(term);  
                        clientContext.ExecuteQuery();  
                        item["TestMeta"] = term.Id.ToString();  
                        item.Update();  
      
                    }  
                    clientContext.ExecuteQuery();  
      
                }  
    

    This is my TermSet settings in Site Collection Term Store:

    30056-snipaste-2020-10-05-10-05-27.png

    Change TermGroup and TermSet Group name with yours.

    And here is the field value settings (TestChoice and TestMeta field)

    30061-snipaste-2020-10-05-10-06-49.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful