Introduction:
Location based metadata defaults manages the default values of metadata fields based on location and applies them so that they are available when the user edits a document. SharePoint 2010 allows setting the default metadata values to the document library, folders and sub folders, so that users do not have to enter the metadata values when they upload the documents in the document library. Please refer http://msdn.microsoft.com/en-us/library/ee557925.aspx for location-based metadata defaults on columns that support setting defaults, in a hierarchy of folders. In this article we will be seeing how to get and set metadata default values using SharePoint object model.
Description:
I have a document library "Shared Documents" which has folders and sub folders as shown in the following figure.
I have created single line of text column in Shared Documents which will be used for setting metadata defaults for the folder and sub folders.
Shared Documents has the following columns
For the Finance folder I have set the metadata default value as Finance.
MetadataDefaults Class:
MetadataDefaults class is used to provide a way to set and get default values for fields based on where the document is added.
Namespace: Microsoft.Office.DocumentManagement
Assembly: Microsoft.Office.DocumentManagement (in Microsoft.Office.DocumentManagement.dll)
MetadataDefaults Constructor (SPList):
This is used to initialize a new instance of a object.
In this article we will be seeing the following
Gets the default value for a specified folder and field.
Gets the default value for a specified field and folder.
Removes all of the default values set on the document library.
Removes all of the defaults set at a specified location.
Removes all of the default values that are set at a specified location.
Removes the default value of aSPField object at a specified SPFolder object.
Removes the default value of a field from an SPFolder object.
Sets a default for a field at a location.
Sets a default value for a default field to apply to documents at a specified location.
Create a Console Application:
Open Visual Studio 2010,
Go to File => New => Project.
Select Console Application template from the installed templates.
Enter the name for the project and click on Ok.
Add the following reference
Microsoft.SharePoint
Microsoft.Office.DocumentManagement
Add the following Namespaces
Using Microsoft.SharePoint;
Using Microsoft.Office.DocumentManagement;
Gets the default value for a specified folder and field:
Replace Program.cs with the following code
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
SPFolder folder = null;
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
folder = web.GetFolder("Shared Documents/Finance");
string defaultValue = metadata.GetFieldDefault(folder, "Default");
Console.WriteLine("Default Value : {0}", defaultValue);
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title+ " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Gets the default value for a specified field and folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();
string defaultValue = metadata.GetFieldDefault(folderPath, "Test");
Console.WriteLine("Default Value : {0}", defaultValue);
Console.ReadLine();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title + " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Removes the default value of a SPField object at a specified SPFolder object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
SPFolder folder = null;
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
folder = web.GetFolder("Shared Documents/Finance");
metadata.RemoveFieldDefault(folder, "Test");
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title + " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
For Finance folder the default metadata value will be removed.
Removes the default value of a field from an SPFolder object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();
metadata.RemoveFieldDefault(folderPath, "Test");
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title + " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
} For Finance folder the default metadata value will be removed.
Removes all of the defaults set at a specified location
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint; using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata { class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://servername:1111/hr/MP/")) { using (SPWeb web = site.RootWeb) { SPList list = web.Lists.TryGetList("Shared Documents"); if (list != null) { MetadataDefaults metadata = new MetadataDefaults(list); SPFolder folder = null; if (web.GetFolder("Shared Documents/Finance").Exists == true) { folder = web.GetFolder("Shared Documents/Finance"); metadata.RemoveAllFieldDefaults(folder); metadata.Update(); } else { Console.WriteLine(" Folder does not exists in the document library"); Console.ReadLine(); } } else { Console.WriteLine(list.Title + " does not exists in the site"); Console.ReadLine(); } } } } } }
Removes all of the default values that are set at a specified location
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();
metadata.RemoveAllFieldDefaults(folderPath);
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title + " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Removes all of the default values set on the document library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
metadata.RemoveAllDefaults();
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title+ " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Sets a default for a field at a location
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
SPFolder folder = null;
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
folder = web.GetFolder("Shared Documents/Finance");
metadata.SetFieldDefault(folder, "Test", "Finance");
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title+ " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Sets a default value for a default field to apply to documents at a specified location
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement;
namespace LocationBasedMetadata
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
MetadataDefaults metadata = new MetadataDefaults(list);
if (web.GetFolder("Shared Documents/Finance").Exists == true)
{
string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();
metadata.SetFieldDefault(folderPath, "Test", "Finance");
metadata.Update();
}
else
{
Console.WriteLine(" Folder does not exists in the document library");
Console.ReadLine();
}
}
else
{
Console.WriteLine(list.Title+ " does not exists in the site");
Console.ReadLine();
}
}
}
}
}
}
Comments