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:
Change TermGroup and TermSet Group name with yours.
And here is the field value settings (TestChoice and TestMeta field)
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.