0

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!

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.