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
2 Answers
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:
- Get the SspId and TermSetId from a Managed Metadata column in a custom content-type.
- Use this information to execute the GetTermSets method on the Taxonomy Webservice and parse the result into a usable list or tree.
- 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
-
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? Commented Aug 29, 2012 at 18:49
-
1Setting the data is very easy, just use item["MyManagedMetadataColumn"] = item.ToString(); Commented Aug 29, 2012 at 18:53
-
thanks again but now i get value does not fall within expected range Commented Aug 29, 2012 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 Commented Aug 29, 2012 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.– JohannesCommented Oct 27, 2013 at 8:33
Yes this is possible. Please have a look at the following link:
-
1Thanks 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. Commented Aug 13, 2012 at 19:25