Not easy, no way…but just like some of my fellow SharePoint experts, we persist and conquor! Metadata Service is awesome, just basic SOA pattern in yet another different implementation. Nothing special, but working with a specific implementation brings interesting challenges…as always. So how do you update a list item's column value when its a metadata column? Some assumptions first:
- You have setup a Metadata Service Application and Proxy
- You have setup a TermStore with a Group called "All Sites"
- You have two termsets defined with values ("Department" and "ITRequestType")
- You have a site associated to the metadata service
- You have a list called "UserRequest" with two meta data columns pointing to your termsets
NOTE: A precaution, in order for a metadata term to have a WSSID, it must have been used and a listitem added to the "TaxonomyHiddenList" list. It is hidden, so you can't see, and you can't update it to be "unhidden" as it seems there is an eventhandler on it to keep you from doing so! I dumped the list fields out, but it might be tricky to figure out how to add a listitem to the list. Something to be done later 🙂
Here's the code:
$site = new-object Microsoft.SharePoint.SPSite("http://intranet.contoso.com/sites/it")
$web = $site.rootweb
$list = $web.Lists["UserRequests"]
$department_field = $list.fields["Department"]
$itrequesttype_field = $list.fields["RequestType"]
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site);
$termstore = $session.TermStores[0]
$group = $termstore.groups["all sites"]
$departmenttermset = $group.termsets["department"]
$requesttypetermset = $group.termsets["itrequesttype"]
$term = $departmenttermset.terms["it"]
$t_it = new-object Microsoft.sharepoint.taxonomy.taxonomyfieldvalue($department_field)
$t_it.wssid = [Microsoft.sharepoint.taxonomy.taxonomyfield]::getwssidsofterm($site,$termstore.id,$departmenttermset.id, $term.id,$false,1)[0]
$t_it.TermGuid = $term.id
$t_it.label = $term.name
$newitem = $list.items.Add()
$newitem["Title"] = "A new item";
$newitem.update()
$newitem = $list.items.getitembyid($newitem.id)
$newitem["Department"] = $t_it
$newitem.update()
Refresh the list on the site, you should see the label show up for the metadata column! Sweet!
Enjoy this one, I'm proud of it!
Chris