Hi everyone,
There is library in SharePoint 2013 with a Multi choice Managed Metadata Field named: "OccupationMetadata"
This Metadata field is connected to "Occupation" Termset in Managed Metadata Service, which is an open termset ( Submission Policy = Open )
The "OccupationMetadata" field has been set as -> Allow Fill-in = Yes
So users can add to this termset in the library.
There is a string named: "OccupationsString". (This string has some sub-strings, which are separated with ; )
e.g. OccupationsString= "S1;S2;S3;S4"
When adding a new document to this library, my code needs to update "OccupationMetadata" field as bellow:
- If "S1" doesn't exist in the "Occupation" Termset, then add "S1" to this termset and then, add "S1" to the "OccupationMetadata" field
- If "S1" already exists in the "Occupation" Termset, then only add "S1" to the "OccupationMetadata" field
Here is my code, but not working!
//Start a taxonomy session
TaxonomySession taxonomySession = new TaxonomySession(elavatedSite);
TermStore Store = taxonomySession.TermStores["Managed Metadata Service"];
Group group = Store.Groups["Group"];
TermSet termSet = group.TermSets["Occupation"];
TermCollection terms = termSet.Terms;
Term ToAddTerm = null;
foreach (var term in terms)
{
if (term.Name == "S1")
{
string TermGuid = Convert.ToString(term.Id);
Guid TermID = new Guid(TermGuid);
ToAddTerm = termSet.GetTerm(TermID);
}
}
item["Title"] = foundItem.Title;
item["SOC Code"] = foundItem.SOCCode;
if(ToAddTerm!= null)
item["OccupationsMetadata"] = ToAddTerm.Terms;
item.Update();
elavatedWeb.AllowUnsafeUpdates = false;To test, in the above code, I only check for term= "S1" .
Could you please advise?
Many thanks