Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Update a Multi choice Managed Metadata Field in a library using c#

$
0
0

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






Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>