<CharlieDigital/> Programming, Politics, and uhh…pineapples


Updating SharePoint Taxonomy Fields

I tripped up on it a bit recently on SharePoint taxonomy fields (or managed metadata fields) as I found that I wasn't able to push down values from a SharePoint list item to a Word document programmatically in SharePoint.

I was able to set the property in SharePoint and the list item would reflect the property in string form like so:


But the problem with this is that when the user opens the document, the Document Information Panel (DIP) will not have the value pre-selected.  All of the other values are set properly and show up in the DIP.  I found that if I saved the item manually in SharePoint through the UI, it would push the changes down to the Word document, but it took quite a while to figure out how to set the taxonomy field values.

In the Word document itself, I could see that the document properties were clearly different.  The first code block shows what a functioning document properties XML looks like:

<RegionTaxHTField0 xmlns="3a290427-a0ba-4a8f-bf50-36a9ad2bef07">
    <Terms xmlns="http://schemas.microsoft.com/office/infopath/2007/PartnerControls">
        <TermInfo xmlns="http://schemas.microsoft.com/office/infopath/2007/PartnerControls">

And here is what it looks like if you set the property using the string value only:

<RegionTaxHTField0 xmlns="3a290427-a0ba-4a8f-bf50-36a9ad2bef07">
    <Terms xmlns="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"></Terms>

I chased this around for hours trying to figure out how to set the field values in SharePoint so that they'll be pushed down into the Word document.  It turns out, there's a new API for handling taxonomy fields.  The following sample assumes that you've iterated through the fields and collected all of the taxonomy fields:

/// <summary>
///   Updates the taxonomy fields.
/// </summary>
/// <param name="target">The target file.</param>
/// <param name="taxonomyFields">
///   The taxonomy fields which have been collected already.
/// </param>
private static void UpdateTaxonomyFields(SPFile target,
    List<TaxonomyField> taxonomyFields)
    SPListItem item = target.Item;
    SPSite site = item.Web.Site;

    TaxonomySession session = new TaxonomySession(site, false);

    foreach(TaxonomyField t in taxonomyFields)
        TermStore termStore = session.TermStores[t.SspId];

        TermSet termSet = termStore.GetTermSet(t.TermSetId);

        string value = Convert.ToString(item[t.Id]);

        string[] parts = value.Split('|');

        value = parts[parts.Length - 1];

        Term term = termSet.GetTerm(new Guid(value));

        if(term == null)
            Log.Write("Term could not be found for value \"{0}\"", value);

        t.SetFieldValue(item, term);

Using this approach, you can set the property in SharePoint and push it down to the document as well.

Posted by Charles Chen

Filed under: .Net, SharePoint Comments Off
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.