Set hold to documents based on Metadata value in SharePoint 2010


In this article we will be seeing how to set hold to documents based on Metadata values in SharePoint 2010.

In "Shared Documents" I have created metadata column "Metadata Col". Based on the metadata value documents will be set to hold.

Shared Documents has the following documents and documents having the "Metadata Col" value as "Friday" will be set to hold.

Share1.gif

Steps Involved:

  • Open Visual Studio 2010.
  • Go to File => New => Project.
  • Select "Console Application" template from the installed templates.
  • Add the following references.

    • Microsoft.SharePoint.dll
    • Microsoft.Office.Policy.dll
     
  • Add the following namespaces.

    • using Microsoft.SharePoint;
    • using Microsoft.Office.RecordsManagement.Holds;
     
  • Replace Program.cs with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.Office.RecordsManagement.Holds; 

    namespace Holds
    {
        class Program
        {

            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://serverName:22222/sites/TestSite/"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists["Shared Documents"];
                        SPListItemCollection itemColl = list.Items;
                        foreach (SPListItem item in itemColl)
                        {
                            string value = item["Metadata Col"].ToString();
                            string[] metadataValues = value.Split('|');
                            if (metadataValues[0] == "Friday")
                            {
                                SPList listHolds = web.Lists["Holds"];
                                SPListItem itemHold = listHolds.Items[0];
                                Hold.SetHold(item, itemHold, "Hold added");
                            }
                        }
                    }
                }
            }
        }
    }

     

Up Next
    Ebook Download
    View all
    Learn
    View all