SharePoint有很多功能,这些功能都有各自的特性,例如管理文档的record center,可以设置文件的传送(routing),将一些符合传送规则(routing rule)的文档,自动传送到其他的文档库中,来管理文档的生命周期。
下面的代码是根据传送规则,将Drop Off Library下面的文档,传送到指定的目标文档库中。
首先需要引入“Microsoft.Office.RecordsManagement.RecordsRepository”命名空间:
using Microsoft.Office.RecordsManagement.RecordsRepository;
以下是具体代码:
SPWeb web = SPContext.Current.Web;
SPList dropOffLib = web.Lists["Drop Off Library"];
EcmDocumentRoutingWeb routingWeb = new EcmDocumentRoutingWeb(web);
EcmDocumentRouter router = routingWeb.Router;
string finalDestination;
bool wasRoutedToOtherWeb;
if (routingWeb.IsRoutingEnabled) //是否启用了传送 routing
{
foreach (SPListItem item in dropOffLib.Items)
{
Response.Write("Item: " + item.DisplayName);
SPFile file = item.File;
bool enableModeration = item.ParentList.EnableModeration; //是否启用审批
bool fileModified = false; //在传送之前需要先checkin和approve
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.CheckIn(string.Empty);
fileModified = true;
}
if (enableModeration && item.DoesUserHavePermissions(SPBasePermissions.ApproveItems))
{
file.Approve(string.Empty);
fileModified = true;
}
SPListItem newItem = item;
if (fileModified)
{
newItem = web.GetListItem(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, item.Url));
}
bool isApproved= true;
if (enableModeration)
{
isApproved = (newItem.ModerationInformation != null) && (newItem.ModerationInformation.Status == SPModerationStatusType.Approved);
}
if (isApproved) //必须是approve的状态才可以传送
{
bool success = router.RouteFileToFinalDestination(newItem, out finalDestination,out wasRoutedToOtherWeb); //输出传送的结果
Response.Write(string.Format("Route result: {0} ", success));
Response.Write(string.Format("finalDestination: {0} ", finalDestination));
Response.Write(string.Format("wasRoutedToOtherWeb: {0} ", wasRoutedToOtherWeb));
Response.Write("</br>");
}
}
}
因为方法“RouteFileToFinalDestination”中使用了SPContext.Current,因此这段代码只能在SharePoint上下文中使用。