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.


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

  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:

  • 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

  1. Microsoft.SharePoint

  2. Microsoft.Office.DocumentManagement

  • Add the following Namespaces

  1. Using Microsoft.SharePoint;

  2. Using Microsoft.Office.DocumentManagement;


Gets the default value for a specified folder and field:



Replace Program.cs with the following code

  1. namespace LocationBasedMetadata

  2. {

  3. class Program

  4.     {

  5. static void Main(string[] args)

  6.         {

  7. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  8.             {

  9. using (SPWeb web = site.RootWeb)

  10.                 {

  11. SPList list = web.Lists.TryGetList("Shared Documents");

  12. if (list != null)

  13.                     {

  14. MetadataDefaults metadata = new MetadataDefaults(list);

  15. SPFolder folder = null;

  16. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  17.                         {

  18.                             folder = web.GetFolder("Shared Documents/Finance");

  19. string defaultValue = metadata.GetFieldDefault(folder, "Default");

  20. Console.WriteLine("Default Value : {0}", defaultValue);

  21.                         }

  22. else

  23.                         {

  24. Console.WriteLine(" Folder does not exists in the document library");

  25. Console.ReadLine();

  26.                         }

  27.                     }

  28. else

  29.                     {

  30. Console.WriteLine(list.Title+ " does not exists in the site");

  31. Console.ReadLine();

  32.                     }

  33.                 }

  34.             }

  35.         }

  36.     }

  37. }






Gets the default value for a specified field and folder:


  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);

  21. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  22.                         {

  23. string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();

  24. string defaultValue = metadata.GetFieldDefault(folderPath, "Test");

  25. Console.WriteLine("Default Value : {0}", defaultValue);

  26. Console.ReadLine();

  27.                         }

  28. else

  29.                         {

  30. Console.WriteLine(" Folder does not exists in the document library");

  31. Console.ReadLine();

  32.                         }

  33.                     }

  34. else

  35.                     {

  36. Console.WriteLine(list.Title + " does not exists in the site");

  37. Console.ReadLine();

  38.                     }

  39.                 }

  40.             }

  41.         }

  42.     }

  43. }






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


  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);

  21. SPFolder folder = null;

  22. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  23.                         {

  24.                             folder = web.GetFolder("Shared Documents/Finance");

  25.                             metadata.RemoveFieldDefault(folder, "Test");

  26.                             metadata.Update();

  27.                         }

  28. else

  29.                         {

  30. Console.WriteLine(" Folder does not exists in the document library");

  31. Console.ReadLine();

  32.                         }

  33.                     }

  34. else

  35.                     {

  36. Console.WriteLine(list.Title + " does not exists in the site");

  37. Console.ReadLine();

  38.                     }

  39.                 }

  40.             }

  41.         }

  42.     }

  43. }

For Finance folder the default metadata value will be removed.



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



  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);

  21. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  22.                         {

  23. string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();

  24.                             metadata.RemoveFieldDefault(folderPath, "Test");

  25.                             metadata.Update();

  26.                         }

  27. else

  28.                         {

  29. Console.WriteLine(" Folder does not exists in the document library");

  30. Console.ReadLine();

  31.                         }

  32.                     }

  33. else

  34.                     {

  35. Console.WriteLine(list.Title + " does not exists in the site");

  36. Console.ReadLine();

  37.                     }

  38.                 }

  39.             }

  40.         }

  41.     }

  42. } For Finance folder the default metadata value will be removed.


Removes all of the defaults set at a specified location



  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

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

  6. 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



  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);

  21. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  22.                         {

  23. string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();

  24.                             metadata.RemoveAllFieldDefaults(folderPath);

  25.                             metadata.Update();

  26.                         }

  27. else

  28.                         {

  29. Console.WriteLine(" Folder does not exists in the document library");

  30. Console.ReadLine();

  31.                         }

  32.                     }

  33. else

  34.                     {

  35. Console.WriteLine(list.Title + " does not exists in the site");

  36. Console.ReadLine();

  37.                     }

  38.                 }

  39.             }

  40.         }

  41.     }

  42. }

Removes all of the default values set on the document library

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);                       

  21. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  22.                         {                                                       

  23. metadata.RemoveAllDefaults();

  24. metadata.Update();  

  25.                                                   }

  26. else                     

  27. {

  28. Console.WriteLine(" Folder does not exists in the document library");

  29. Console.ReadLine();

  30.                         }

  31.                     }

  32. else

  33.                     {

  34. Console.WriteLine(list.Title+ " does not exists in the site");

  35. Console.ReadLine();

  36.                     }

  37.                 }

  38.             }

  39.         }

  40.     }

  41. }

Sets a default for a field at a location



  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10.     {

  11. static void Main(string[] args)

  12.         {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.             {

  15. using (SPWeb web = site.RootWeb)

  16.                 {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.                     {

  20. MetadataDefaults metadata = new MetadataDefaults(list);

  21. SPFolder folder = null;

  22. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  23.                         {

  24.                             folder = web.GetFolder("Shared Documents/Finance");                          

  25.                             metadata.SetFieldDefault(folder, "Test", "Finance");

  26.                             metadata.Update();                           

  27.                         }

  28. else

  29.                         {

  30. Console.WriteLine(" Folder does not exists in the document library");

  31. Console.ReadLine();

  32.                         }

  33.                     }

  34. else

  35.                     {

  36. Console.WriteLine(list.Title+ " does not exists in the site");

  37. Console.ReadLine();

  38.                     }

  39.                 }

  40.             }

  41.         }

  42.     }

  43. }


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


  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.Office.DocumentManagement;

  7. namespace LocationBasedMetadata

  8. {

  9. class Program

  10. {

  11. static void Main(string[] args)

  12.   {

  13. using (SPSite site = new SPSite("http://servername:1111/hr/MP/"))

  14.  {

  15. using (SPWeb web = site.RootWeb)

  16.  {

  17. SPList list = web.Lists.TryGetList("Shared Documents");

  18. if (list != null)

  19.   {

  20. MetadataDefaults metadata = new MetadataDefaults(list);                       

  21. if (web.GetFolder("Shared Documents/Finance").Exists == true)

  22.   {

  23. string folderPath = web.GetFolder("Shared Documents/Finance").ServerRelativeUrl.ToString();

  24. metadata.SetFieldDefault(folderPath, "Test", "Finance");

  25. metadata.Update();                           

  26.  }

  27. else

  28.   {

  29. Console.WriteLine(" Folder does not exists in the document library");

  30. Console.ReadLine();

  31.   }

  32.    }

  33. else

  34.   {

  35. Console.WriteLine(list.Title+ " does not exists in the site");

  36. Console.ReadLine();

  37.   }

  38.   }

  39.   }

  40.  }

  41. }

  42. }