Question

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).

Was it helpful?

Solution

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

        }

OTHER TIPS

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!

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top