SIGN UP MEMBER LOGIN:    
ARTICLE

Programmatically configure Location based metadata defaults in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint July 18, 2011
In this article we will be seeing how to get and set metadata default values using SharePoint object model.
Reader Level:

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 aSPField 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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor