using(SPSite site = new SPSite("http://myspsite")) {
using(SPWeb web = site.OpenWeb()) {
//Name of list - testlist
SPList list = web.Lists["testlist"];
//Get item ID - 2
SPListItem listItem = web.Lists["testlist"].GetItemById(2);
//Get column nameof list - testlist
Guid Promotypeid = listItem.Fields["testlist"].Id;
TaxonomyField taxfield = listItem.Fields[Promotypeid] as TaxonomyField;
TaxonomySession session = new TaxonomySession(site);
TermStore mytermstore = session.DefaultKeywordsTermStore;
TermSet termSet = mytermstore.GetTermSet(taxfield.TermSetId);
Guid termGuid = Guid.Empty;
//update item string value from Term set (value must exist in term set)
string ChangeValue = "Pulsar 250";
//Find the term from tremset and get GUID
foreach(Term term in termSet.Terms) {
if (term.Name.Equals(ChangeValue, StringComparison.OrdinalIgnoreCase)) {
termGuid = term.Id;
break;
}
}
if (termGuid != Guid.Empty) {
Term customTerm = termSet.GetTerm(termGuid);
string taxFieldInternalname = listItem.Fields[taxfield.TextField].InternalName;
listItem[taxFieldInternalname] = customTerm.Name + "|" + customTerm.Id.ToString();
taxfield.SetFieldValue(listItem, customTerm);
web.AllowUnsafeUpdates = true;
listItem.Update();
web.AllowUnsafeUpdates = false;
}
}
}This code show you how to Update SharePoint list Metadata Column programmatically.
A Managed Metadata column is a new column type that can be added to lists, libraries, or content types to enable site users to select values from a specific term set of managed terms and apply these values to their content. You can create and configure a Managed Metadata column to map to an existing term set or term, or you can create a new term set specifically for a Managed Metadata column.
