SharePoint 2010 Hosting :: Setting Default Column Values on a Folder Programmatically

Posted by mbridge on Geeks with Blogs See other posts from Geeks with Blogs or by mbridge
Published on Wed, 12 Jan 2011 04:42:31 GMT Indexed on 2011/01/12 4:54 UTC
Read the original article Hit count: 418

Filed under:
The reason I write this post today is because my initial searches on the Internet provided me with nothing on the topic.  I was hoping to find a reference to the SDK but I didn’t have any luck.  What I want to do is set a default column value on an existing folder so that new items in that folder automatically inherit that value.  It’s actually pretty easy to do once you know what the class is called in the API.  I did some digging and discovered that class is MetadataDefaults. It can be found in Microsoft.Office.DocumentManagement.dll.  Note: if you can’t find it in the GAC, this DLL is in the 14/CONFIG/BIN folder and not the 14/ISAPI folder.  Add a reference to this DLL in your project.  In my case, I am building a console application, but you might put this in an event receiver or workflow.

In my example today, I have simple custom folder and document content types.  I have one shared site column called DocumentType.  I have a document library which each of these content types registered.  In my document library, I have a folder named Test and I want to set its default column values using code.  Here is what it looks like.  Start by getting a reference to the list in question.  This assumes you already have a SPWeb object.  In my case I have created it and it is called site.
SPList customDocumentLibrary = site.Lists["CustomDocuments"];
You then pass the SPList object to the MetadataDefaults constructor.
MetadataDefaults columnDefaults = new MetadataDefaults(customDocumentLibrary);
Now I just need to get my SPFolder object in question and pass it to the meethod SetFieldDefault.  This takes a SPFolder object, a string with the name of the SPField to set the default on, and finally the value of the default (in my case “Memo”).
SPFolder testFolder = customDocumentLibrary.RootFolder.SubFolders["Test"];
columnDefaults.SetFieldDefault(testFolder, "DocumentType", "Memo");
You can set multiple defaults here.  When you’re done, you will need to call .Update().
columnDefaults.Update();
Here is what it all looks like together.
using (SPSite siteCollection = new SPSite("http://sp2010/sites/ECMSource"))
{
    using (SPWeb site = siteCollection.OpenWeb())
    {
        SPList customDocumentLibrary = site.Lists["CustomDocuments"];
        MetadataDefaults columnDefaults = new MetadataDefaults(customDocumentLibrary); 
        SPFolder testFolder = customDocumentLibrary.RootFolder.SubFolders["Test"];
        columnDefaults.SetFieldDefault(testFolder, "DocumentType", "Memo");
        columnDefaults.Update();
    }
}
You can verify that your property was set correctly on the Change Default Column Values page in your list
This is something that I could see used a lot on an ItemEventReceiver attached to a folder to do metadata inheritance.  Whenever, the user changed the value of the folder’s property, you could have it update the default.  Your code might look something
columnDefaults.SetFieldDefault(properties.ListItem.Folder, "MyField", properties.ListItem["
This is a great way to keep the child items updated any time the value a folder’s property changes.  I’m also wondering if this can be done via CAML.  I tried saving a site template, but after importing I got an error on the default values page.  I’ll keep looking and let you know what I find out.

© Geeks with Blogs or respective owner