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

I have a scenario where I'm provisioning a document on the server after collecting some metadata.

Environment is SharePoint 2010 and Office 2010.

The SharePoint list item and file are created correctly. The SharePoint list item has all of the proper metadata assigned in SharePoint.

However, when opening the document in Word 2010, the DIP shows that the managed metadata fields are blank, even though they have the correct value in SharePoint. If I save the item in SharePoint through the UI, it works like a charm and the managed metadata properties (taxonomy fields) are fine if I open it in Word again.

If I try to call SPFile.Update(), SPListItem.SystemUpdate() or SPListItem.Update(), it has no effect on pushing the properties down to the document.

I've found that in the case where the managed metadata properties don't stick, the XML of the document looks like this:

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

After saving the item in the UI, the XML looks like this:

    <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">
                <TermName>Japan</TermName>
                <TermId>c2a154e3-8bb0-4b56-b083-297526964fd9</TermId>
            </TermInfo>
        </Terms>
    </RegionTaxHTField0>

Unforunately, there isn't a whole lot of documentation on this TermInfo element out there on the web. I'm trying to figure out how I can get SharePoint to generate those extra elements (instead of doing it in OpenXML) since, obviously, SharePoint can do it.

Ideas and pointers would be much appreciated!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Resolved!

Need to use the SetFieldValue method of the TaxonomyField class in Microsoft.SharePoint.Taxonomy.dll:

    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);
                continue;
            }

            t.SetFieldValue(item, term);
        }
    }
share|improve this answer

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.