How to: Set Default Column Values on a folder programmatically

Posted Monday, July 19, 2010 3:51 PM by CoreyRoth

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

SiteColumnDefaultValues

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 like this.

columnDefaults.SetFieldDefault(properties.ListItem.Folder, "MyField", properties.ListItem["MyField"].ToString());

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.

Comments

# Twitter Trackbacks for SharePoint Post: How to: Set Default Column Values on a folder programmatically [dotnetmafia.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 SharePoint Post: How to: Set Default Column Values on a folder programmatically         [dotnetmafia.com]        on Topsy.com

# How to set default columns values against a list or library. @ The Buzz Blog

Pingback from  How to set default columns values against a list or library.  @  The Buzz Blog

# re: How to: Set Default Column Values on a folder programmatically

Friday, November 18, 2011 8:30 AM by Harry

Good article Corey thanks.

Only thing I'd add is that all url and folder names are case sensitive for this method.

regards

# re: How to: Set Default Column Values on a folder programmatically

Friday, July 20, 2012 10:03 AM by Andy

Hey Corey, I have two folders (Folder A and B) in a document library and I am using "Column default value settings " from the UI to set default values for the column called "SPTEST" (Text field).

Folder    -- Default Col value

----------------------------------------

Folder A -- Value A (default value of SPTEST)

Folder B -- Value B (default value of SPTEST)

Now If I download a document from Folder B and re-upload it to Folder A , the column value is not changed ? In fact I expected the document to have "Value A" instead of "Value B" ! Is there any OOB way to fix this? If not, I think we need to write EventReceiver to resolve the issue.

Could you please help me !

# re: How to: Set Default Column Values on a folder programmatically

Monday, August 6, 2012 2:20 PM by CoreyRoth

@Andy You'll have to use an Event Receiver in this case.  Default column values only apply when the field is empty.  

# re: How to: Set Default Column Values on a folder programmatically

Monday, October 22, 2012 8:26 AM by Tim O'Pry

Corey -

Do you know if this functionality (setting folder defaults) is possible within SharePoint Online - either 2010 or 2013 versions? The DocumentManagement namespace isn't accessible via the CSOM and I've been searching through the webservices without success.

Any info (even if it's not possible) would be helpful.

# re: How to: Set Default Column Values on a folder programmatically

Tuesday, August 12, 2014 5:27 AM by Paul

Do you know if it is possible to do this sing the CSOM?

# re: How to: Set Default Column Values on a folder programmatically

Friday, August 29, 2014 3:07 PM by CoreyRoth

@Paul afraid not.

# re: How to: Set Default Column Values on a folder programmatically

Tuesday, April 21, 2015 1:28 AM by rohit d

Is ther eany way to update this through shrepoint designer workflow?

# re: How to: Set Default Column Values on a folder programmatically

Saturday, May 2, 2015 12:06 PM by CoreyRoth

@Rohit unfortunately not.

# re: How to: Set Default Column Values on a folder programmatically

Tuesday, June 30, 2015 4:00 PM by Fred

I've been trying to do this for days and I have very little support from IT.

Can I use visual studio to turn this into a workflow activity? Do you know a good tutorial on how to add C# snippets like this to workflows? Can I then add that activity to SPD actions?

I'm seriously dying here. I should not be in charge of this project, but I am.

# re: How to: Set Default Column Values on a folder programmatically

Friday, March 31, 2017 4:41 AM by Matt Smith

Hi Corey, Just wondered if this is now possible on SP online?  its been a few years since your post and I still need this functionality on SP online 365.  Any ideas?

Leave a Comment

(required)
(required)
(optional)
(required)