ACS Blogs

A blog site for Architecting Connected Systems staff to tell the world about their exploits in
SharePoint 2007/2010, Windows Workflow Foundation (3.0/4.0) and other great technologies!
Welcome to ACS Blogs Sign in | Join | Help
in Search

CJG

Updating List Item Metadata Column Value

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:

  1. You have setup a Metadata Service Application and Proxy 
  2. You have setup a TermStore with a Group called "All Sites"
  3. You have two termsets defined with values ("Department" and "ITRequestType")
  4. You have a site associated to the metadata service
  5. 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

Published Tuesday, February 02, 2010 9:47 PM by cjg

Comments

 

SharePoint 2010: Recopilatorio de enlaces interesantes (V)! « Pasi??n por la tecnolog??a… said:

March 1, 2010 2:39 PM
 

Blog del CIIN said:

Recién estrenado el mes de marzo, aquí os dejo una nueva entrega del recopilatorio de recursos

March 1, 2010 2:41 PM
 

mgerats said:

Great post.

The script works great when the term has already been used once and is present in the TaxonomyHiddenList, however not all the terms I want to use are in there yet. When the term is not there, GetWssIDOfTerm returns 0;#TermName.

Is there any way to programmatically populate the HiddenInformationList from the Term Store?

May 3, 2010 1:34 AM
Anonymous comments are disabled

This Blog

Syndication

Powered by Community Server, by Telligent Systems