By default, SharePoint folders do not support columns (metadata). They behave very much like Windows explorer folders with limited config options compared to document libraries. However, SharePoint folders exist inside document libraries, and document libraries have full support for columns (also called fields or metadata).
Most column types (like single line of text, and choice, and number) allow for setting default values. And when a default value is set, any new item uploaded to the library will automatically inherit that default value for the field in question.
Scenario
Let’s say you have a document library called DocLib with multiple folders – FolderOne, FolderTwo, and FolderThree. This document library also has a column called DocumentType. Now, depending on the folder into which an item is uploaded, you may want to set the value of the DocumentType column for the newly uploaded item. So instead of a global column default value, you want your default values to be folder-specific.
Let’s see how this can be done using some custom C# code.
Setting default column values using the MetadataDefaults class
To demonstrate my solution, I will be working with the scenario described above (as in, the document library and folders setup). In addition, I will configure a “global” column default on the DocumentType column and add a fourth folder FolderFour. We won’t change MetadataDefaults on FolderFour. Instead the folder will serve as a “control” to help us see how things would work by default.
Here’s how the setup looks like:
I have configured the default value of the DocumentType column to be “Global Default Value”.
I will be demonstrating this solution on a SharePoint 2016 server using a C# console application. Here’s the full code we will be working with:
using Microsoft.Office.DocumentManagement; using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestConsoleApp { class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://spvm")) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["DocLib"]; MetadataDefaults folderOneColumnDefaults = new MetadataDefaults(list); SPFolder folderOne = list.RootFolder.SubFolders["FolderOne"]; folderOneColumnDefaults.SetFieldDefault(folderOne, "DocumentType", "Folder One Default Value"); folderOneColumnDefaults.Update(); MetadataDefaults folderTwoColumnDefaults = new MetadataDefaults(list); SPFolder folderTwo = list.RootFolder.SubFolders["FolderTwo"]; folderTwoColumnDefaults.SetFieldDefault(folderTwo, "DocumentType", "Folder Two Default Value"); folderTwoColumnDefaults.Update(); MetadataDefaults folderThreeColumnDefaults = new MetadataDefaults(list); SPFolder folderThree = list.RootFolder.SubFolders["FolderThree"]; folderThreeColumnDefaults.SetFieldDefault(folderThree, "DocumentType", "Folder Three Default Value"); folderThreeColumnDefaults.Update(); } } } } }
Note that you will need to add a reference to the Microsoft.Office.DocumentManagement namespace in order to use the MetadataDefaults class.
Run the above code. It only needs to be run once.
Now, manually upload documents into the different folders and note the default values that get set each time. Since our code did not configure MetadataDefaults on FolderFour, you will notice that when items are uploaded into FolderFour, they get the DocumentType default value of “Global Default Value”. This is also what happens when items are uploaded directly into the document library without being put into any named folder.
When items are uploaded in to FolderOne, or FolderTwo, or FolderThree, they get the DocumentType default value of “Folder One Default Value”, or “Folder Two Default Value” or “Folder Three Default Value” respectively.
You can view these default values by going to Document library settings > Column default value settings. Here’s how it looks for FolderOne.
Potential applications of this code could be inside of event receivers to keep default column values on child items updated when something changes somewhere.
Leave a Reply