Programmatically configure Location based metadata defaults in SharePoint 2010


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.

MtDataShare1.gif

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

MtDataShare2.gif

For the Finance folder I have set the metadata default value as Finance.

MtDataShare3.gif

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

  1. Gets the default value for a specified folder and field.
  2. Gets the default value for a specified field and folder.
  3. Removes all of the default values set on the document library.
  4. Removes all of the defaults set at a specified location.
  5. Removes all of the default values that are set at a specified location.
  6. Removes the default value of a SPField object at a specified SPFolder object.
  7. Removes the default value of a field from an SPFolder object.
  8. Sets a default for a field at a location.
  9. Sets a default value for a default field to apply to documents at a specified location.

Create a Console Application:

  1. Open Visual Studio 2010,
  2. Go to File => New => Project.
  3. Select Console Application template from the installed templates.
  4. Enter the name for the project and click on Ok.
  5. Add the following reference

    • Microsoft.SharePoint
    • Microsoft.Office.DocumentManagement
     
  6. Add the following Namespaces

    • Using Microsoft.SharePoint;
    • Using Microsoft.Office.DocumentManagement;
     

Gets the default value for a specified folder and field:

MtDataShare4.gif

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();
                    }
                }
            }
        }
    }
}


MtDataShare5.gif

Gets the default value for a specified field and folder:

MtDataShare6.gif

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();
                    }
                }
            }
        }
    }
}

MtDataShare7.gif

Removes the default value of a SPField object at a specified SPFolder object

MtDataShare8.gif

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.

MtDataShare9.gif

Removes the default value of a field from an SPFolder object.

MtDataShare10.gif

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.

MtDataShare11.gif

Removes all of the defaults set at a specified location

MtDataShare12.gif

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

MtDataShare13.gif

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

MtDataShare14.gif

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();
                    }
                }
            }
        }
    }
}



MtDataShare15.gif

Sets a default value for a default field to apply to documents at a specified location

MtDataShare16.gif

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();
                    }
                }
           }
        }
    }
}


MtDataShare17.gif

Up Next
    Ebook Download
    View all
    Learn
    View all