Column Default Values for SPFolder

You can use this technique, when you want to default the SPField values for items added inside folders.

The default values can be overridden at the top level folders so the items created under them will have these values.


Please add references to the following namespaces

using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;


You can set the default value like below


public static void SetDefaultValue()
{

 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
  using (SPSite site = new SPSite("http://test/sites/reaustralia"))
  {
   using (SPWeb web = site.OpenWeb())
   {
    SPDocumentLibrary Dealslibrary = (SPDocumentLibrary)web.Lists["Deals"];
    MetadataDefaults columndefaults1 = new MetadataDefaults(Dealslibrary);
    SPFolder folder1 = Dealslibrary.RootFolder;
    SPFolderCollection folderCollection1 = folder1.SubFolders;

    //SPFolder , ColumName, Value
    columndefaults1.SetFieldDefault(folderCollection1[1], "COLUMN_NAME", "Value");
    columndefaults1.Update();

   }
  }
 });
}

You can retrieve column default value like below

SPDocumentLibrary library = (SPDocumentLibrary)web.Lists[DocLibName];
SPFolderCollection folderCollection = library.RootFolder.SubFolders;

MetadataDefaults columndefaults = new MetadataDefaults(library);

//Pass the SPFolder you want the metadata for
Pair[] pair = columndefaults.GetDefaultMetadata(folderCollection[1]);

//each pair object has a column-value-pair
string ColumnName = Convert.ToString(pair[i].First);
string Value = Convert.ToString(pair[i].Second);


You can also view the assigned values like below


Library settings , then Change Default Column Values






Have fun !!
Column Default Values for SPFolder Column Default Values for SPFolder Reviewed by Srikant N on Tuesday, April 26, 2016 Rating: 5

No comments :

Thanks for your time ! Have a great day.

Copyright 2016 (Srikant N). Powered by Blogger.