7

I'm trying to set the metadata value for a folder in a document library with the following code:

using (SPSite siteCollection = new SPSite("http://dev.site.com"))
        {
            using (SPWeb web = siteCollection.OpenWeb())
            {
                SPList customDocumentLibrary = web.Lists["Program Documents"];
                MetadataDefaults columnDefaults = new MetadataDefaults(customDocumentLibrary);

                SPFolder testFolder = customDocumentLibrary.RootFolder.SubFolders["Programs"];

                columnDefaults.SetFieldDefault("/Program Documents/Programs/Academics", "Programs", "Academics");
                columnDefaults.Update();
            }

        }

I'm getting the following error:

The given value for a taxonomy field was not formatted in the required <int>;#<label>|<guid> format. 

Does anyone have an example of how to pass in the parameters for SetFieldDefault()? I'm unsure what the int;#label is referring to in the error message (though I have the guid of the taxonomy field).

3 Answers 3

7

So it turns out the value you need to pass in for the taxonomy value is the following:

lcid;#Term Name|Guid of term

e.g.:

1033;#Academics|bc943091-79ac-4f5f-a79b-205e8e717823

The full code to set the metadata column default is here:

        using (SPSite siteCollection = new SPSite("http://www.yoursite.com"))
        {
            using (SPWeb web = siteCollection.OpenWeb("/yourweb/"))
            {

                SPList customDocumentLibrary = web.Lists["Documents"];
                SPFolder rootFolder = customDocumentLibrary.RootFolder;

                MetadataDefaults columnDefaults = new MetadataDefaults(customDocumentLibrary);

                columnDefaults.RemoveAllDefaults();
                columnDefaults.SetFieldDefault(rootFolder, "Programs", "1033;#Academics|bc943091-79ac-4f5f-a79b-205e8e717823");
                columnDefaults.Update();
            }

        }
2

Although it might work fine to provide the LCID as to the default value, it's not what you're supposed to supply. You should supply the ID of the listitem in the hidden list "/Lists/TaxonomyHiddenList" that corresponds to the term you're setting as default. This list is some sort of cache of all the terms you use inside your site, which is explained in more detail over here: http://www.wictorwilen.se/Post/Dissecting-the-SharePoint-2010-Taxonomy-fields.aspx

Hope this helps!

1
  • 1
    This is correct. AFAIK it's also possible to use -1 as the WSS ID and let SharePoint handle that. Jul 29, 2016 at 12:35
0

If you're dealing with multiple taxonomy values the values are separated by ;#

Your Answer

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

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