How to: Create a Custom Solution That Implements the IHold Interface in SharePoint Server 2010 (ECM)

Applies to: SharePoint Server 2010

You can activate the Hold and eDiscovery feature on a site. This feature enables you to search for content in a site collection and add it to a hold for litigation or auditing purposes. This topic introduces the IHoldsHandler interface, which provides an interface that a custom hold handler can implement to provide custom hold processing. Rather than copying content to another SharePoint Server site or locking down documents in place, the custom hold processor exports the search results to a file share.

When a list item is put on hold or removed from hold, you can add a custom processing handler by implementing the IHoldsHandler.OnSetHold(Microsoft.SharePoint.SPListItem,Microsoft.SharePoint.SPListItem) method and the IHoldsHandler.OnRemoveHold(Microsoft.SharePoint.SPListItem,System.Collections.Generic.List{Microsoft.SharePoint.SPListItem}) method in the IHoldsHandler interface. After the handler processes the list item, it can cancel, continue, or skip default processing by returning the corresponding HoldHandlerResult object.

To configure, build, and run the sample

  1. Copy the code example to a Microsoft Visual Studio 2010 project, and then save the file as CustomHold.cs.

  2. Edit CustomHold.cs to specify the destination folder (represented by the \\myserver\myfolder\hold\ placeholder path in the code) where the files included in the search results will be exported to.

  3. Build the sample as a Release project (not Debug).

  4. Create a folder on the computer running SharePoint Server where the custom hold processor will be registered.

  5. From the CustomHoldProcessor\MyNewCustomHoldProcessor\bin\x64\Release folder, copy the contents that were built by this project to the folder that you specified in step 2.

  6. From the registerholdprocesspr\registerholdprocesspr\bin\Release folder, copy the contents that were built by this project to the folder that you specified in step 2.

  7. Navigate to the folder that you created in step 2, and then modify the config.xml file to specify the site collection to register the hold processor for, and the location of the folder that you specified in step 2.

  8. Locate the MyNewCustomHoldProcesser.dll file, and then add it to the Global Assembly Cache (GAC).

  9. Run registerholdprocesspr.exe.

Example

The following code examples include the custom implementation of the IHoldsHandler interface, and include the code required to register the custom hold processor and specify the site to register the hold processor on.

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

//This class specifies the custom hold processor that is used to export the contents
//of a search and process job to a file share. 
namespace Microsoft.SDK.ECM.Samples.WebControls.MyNewCustomHoldProccessor
{
    public class MyHoldProccessor : IHoldsHandler
    {
        //The action to perform when setting the hold; copy the file to the specified destination. 
        public HoldHandlerResult OnSetHold(SPListItem item, SPListItem hold)
        {
            WebClient client = new WebClient();
            client.UseDefaultCredentials = true;
            string source = item.Web.Url + item.File.ServerRelativeUrl;
            //The destination to export files to.
            string destination = @"\\myserver\myfolder\hold\" +item.Name;

            client.DownloadFile(source, destination);           
            return HoldHandlerResult.Cancel;
        }

        //The action to perform when a hold is being released.
        public HoldHandlerResult OnRemoveHold(SPListItem item, List<SPListItem> holds)
        {
            foreach (SPListItem eachitem in holds)
            {
                string str = eachitem.DisplayName;                
            }
            return HoldHandlerResult.SuccessContintueProcessing;
        }
    }
}
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.Office.RecordsManagement;
using Microsoft.Office.RecordsManagement.Holds;

//This class registers the custom hold processor. 
namespace Microsoft.SDK.ECM.Samples.WebControls.RegisterCustomHoldProccessor
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument config = new XmlDocument();
            config.Load("config.xml");

            XmlElement rootEle = config.DocumentElement;
            XmlNodeList nodeList = rootEle.ChildNodes;

            string siteName = "";
            string libLocation = "";

            foreach (XmlNode eachNode in nodeList)
            {
                //This is the site collection to register the hold processor on; this is specified in config.xml.
                if (eachNode.Name == "site")
                {
                    siteName = eachNode.InnerText;
                }
                //This is the location of the files; this is specified in config.xml.
                if (eachNode.Name == "LibLocation")
                {
                    libLocation = eachNode.InnerText;
                }
            }
            Console.WriteLine("Customizing hold processor for " + siteName);
            Console.WriteLine("Search library location " + libLocation);


            SPSite site = new SPSite(siteName);
            Assembly searchengineassembly = Assembly.LoadFile(libLocation);
            Hold.RegisterCustomHoldProcessor(searchengineassembly.FullName, "MyNewCustomHoldProccessor.MyHoldProccessor", site.WebApplication);            
        }
    }
}

See Also

Concepts

Managing eDiscovery in SharePoint Server 2010 (ECM)

Records Management Programming Model in SharePoint Server 2010 (ECM)

Other Resources

Walkthrough: Creating Shared Events