如何:创建实现 IHold 接口的自定义解决方案
上次修改时间: 2010年4月15日
适用范围: SharePoint Server 2010
可以激活网站上的"保留和电子数据展示"功能。利用此功能,可以搜索网站集中的内容并将其添加到保留项,以供诉讼和审计使用。本主题介绍了 IHoldsHandler 接口,该接口提供了一个可供自定义保留项处理程序实现以提供自定义保留项处理的接口。自定义保留项处理器会将搜索结果导出到文件共享,而不是将内容复制到另一个 SharePoint Server 网站或就地锁定文档。
在将列表项置于保留状态或将列表项从保留状态中删除时,可以通过实现 IHoldsHandler 接口中的 IHoldsHandler.OnSetHold(Microsoft.SharePoint.SPListItem,Microsoft.SharePoint.SPListItem) 方法和 IHoldsHandler.OnRemoveHold(Microsoft.SharePoint.SPListItem,System.Collections.Generic.List{Microsoft.SharePoint.SPListItem}) 方法添加自定义处理处理程序。在处理程序处理完列表项后,它可以通过返回相应的 HoldHandlerResult 对象来取消、继续或跳过默认处理。
配置、生成和运行示例
将代码示例复制到 Microsoft Visual Studio 2010 项目,然后将文件另存为 CustomHold.cs。
编辑"CustomHold.cs"以指定搜索结果中包括的文件将导出到的目标文件夹(由代码中的 \\myserver\myfolder\hold\ 占位符路径表示)。
生成作为 Release 项目(而非 Debug)的示例。
在运行 SharePoint Server 的计算机上创建一个将注册自定义保留项处理器的文件夹。
从 CustomHoldProcessor\MyNewCustomHoldProcessor\bin\x64\Release 文件夹中,将此项目所生成的内容复制到步骤 2 中指定的文件夹。
从 registerholdprocesspr\registerholdprocesspr\bin\Release 文件夹中,将此项目生成的内容复制到步骤 2 中指定的文件夹。
导航到步骤 2 中创建的文件夹,然后修改"config.xml"文件以指定要为其注册保留项处理器的网站集,并指定步骤 2 中指定的文件夹的位置。
找到"MyNewCustomHoldProcesser.dll"文件,然后将该文件添加到全局程序集缓存 (GAC)。
运行 registerholdprocesspr.exe。
示例
以下代码示例包含 IHoldsHandler 接口的自定义实现和注册自定义保留项处理器所需的代码,并指定注册保留项处理器的网站。
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);
}
}
}