Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to return the values of a Managed Metadata field assigned to a document using strictly the client object model? Note that I want to return ALL the possible values and allow the user to choose one, perhaps via a treeview or comboboxes, just to let the user assign a predefined value to the field. The user does not need to change the values in the field. I only wantt to display the possible values, allow one to be chosen and assign the chosen value to the field. Thank You, -Derek

share|improve this question

2 Answers 2

I had the same question and searched the internet some code examples. I found some code fragments but no easy & complete working solution. So I created this code to do the two steps:

  1. Get the SspId and TermSetId from a Managed Metadata column in a custom content-type.
  2. Use this information to execute the GetTermSets method on the Taxonomy Webservice and parse the result into a usable list or tree.
  3. Use the value from the MetadataItem when setting the value for a ListItem

Details:
Step 1.

var clientContext = new ClientContext("https://localhost/sites/STS20120731101501");
var properties = clientContext.GetTaxonomyFieldProperties("MyCustomContentType", "MyManagedMetadataColumn");
string sspId = properties["SspId"]; // e40374fb-a6cc-4854-989f-c1d94a7967ee
string termSetId = properties["TermSetId"]; // 9eeec005-4818-486f-b4d8-bda1d43d42b4


Step 2.

using (var client = new TaxonomywebserviceSoapClient())
{
    string sspID = "e40374fb-a6cc-4854-989f-c1d94a7967ee";
    string termsetId = "9eeec005-4818-486f-b4d8-bda1d43d42b4";
    var items = client.GetMetadataItems(sspID, termsetId);

    foreach (var item in items.AsTree())
    {
        Console.WriteLine(new string(' ', item.Level) + item);
    }

    var firstMetadataItem = items.First(); // Just take the first as an example.
}


Step 3.

using (var clientContext = new ClientContext("https://localhost/sites/STS20120731101501"))
{
  var list = clientContext.Web.Lists.GetByTitle("MyList");

  var newListItem = list.AddItem(new ListItemCreationInformation());
  newListItem["SomeNormalField"] = "SomeText";
  newListItem["MyManagedMetadataColumn"] = firstMetadataItem.ToString(); // This will be like "MyFirstValue|643b827f-5fed-4a34-8270-ed0c662df639"

  newListItem.Update();
  clientContext.ExecuteQuery();
}


The extension methods : GetTaxonomyFieldProperties, GetMetadataItems and AsTree can be found here.


References

share|improve this answer
    
thanks, i've got the tree sorted out but i can't for the life of me figure out how to then set the value of the field. i've got lots of samples but they are all converted from c# maybe something was lost along the way? –  Derek Drew Aug 29 '12 at 18:49
1  
Setting the data is very easy, just use item["MyManagedMetadataColumn"] = item.ToString(); –  Stef Aug 29 '12 at 18:53
    
thanks again but now i get value does not fall within expected range –  Derek Drew Aug 29 '12 at 19:42
    
If fldtypes(i) = "TaxonomyFieldType" Then list1(fldnames(i)) = tv.SelectedNode.Text.ToString End If <br>listl.update <br> br represents line breaks since everytime i hit enter it just posts context.executequery –  Derek Drew Aug 29 '12 at 19:43
    
This helped me a lot, even though some tweaks was needed to make it work in SharePoint 2013. For example the TaxonomywebserviceSoapClient seem to have changed name to Taxonomywebservice. –  Johannes Oct 27 '13 at 8:33

Yes this is possible. Please have a look at the following link:

http://blogs.msdn.com/b/sharepointdev/archive/2011/11/18/how-to-work-with-managed-metadata-columns-by-using-the-sharepoint-client-object-model-kaushalendra-kumar.aspx

share|improve this answer
1  
Thanks a lot but i don't want to use TaxonomyHiddenList, as that is only populated with values that have been assigned to a document in the past. I want ALL possible terms. However I am new to Sharepoint so it's possible I just misunderstood something in the article you posted. Please let me know. –  Derek Drew Aug 13 '12 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.