如何:使用后操作和绕过锁使操作过期

上次修改时间: 2010年8月10日

适用范围: SharePoint Server 2010

正确处理记录时需要正确处理自定义策略操作。因为在未正确注册或使用自定义策略操作时,对象模型几乎不提供警告或错误,所以处理自定义策略操作时需要更深入地了解对象模型的工作方式。

本主题提供的提示和代码示例可帮助您使用对象模型来终止包含后期操作和绕过锁定的操作。由于保留策略在所有情况下都应该能对记录起作用,因此建议您编写自定义操作,以便即使所编写的特定操作不用于处理记录,保留策略也可以管理记录。本主题介绍如何在实现 OnExpirationWithPostActions(SPListItem, XmlNode, Time) 方法以处理记录的代码中包装自定义策略操作,以及如何在不希望代码处理锁定记录时绕过对记录的锁定。

  1. 如果使用新增的 IExpirationActionWithPostActions 接口并实现 OnExpirationWithPostActions(SPListItem, XmlNode, Time) 方法,则返回正确的 ExpirationPostActions 操作以更新 SPItem 对象(如果它是记录)。

  2. 如果使用 IExpirationAction 接口或仅实现 Expiration 类,则 Microsoft SharePoint Server 2010 中的记录管理解决方案默认情况下会正确管理自定义策略操作。

示例

ExpirationPostActions 示例演示了如何声明后期操作,以便在某个项目过期时对其执行该操作。代码会重新计算项目的过期日期,更新该项目,并在运行用户定义的自定义操作后为该操作记录一个审核事件。

代码还会查看"就地记录管理"功能是否锁定了该项目。代码使用后期操作放弃签出到系统帐户的记录,更新该项目,然后再次将该项目签出到系统帐户。

public ExpirationPostActions OnExpirationWithPostActions(SPListItem item, XmlNode parametersData, DateTime expiredDate)
{
    /* Recalculates the item's expiration date, updates 
     * the item, and logs an audit event for the action. */
    ExpirationPostActions postActions = ExpirationPostActions.Default;

    //Do whatever you want to do on the item.

    //Check to see whether In-Place Records Management has locked the item.
    if (Records.IsLocked(item))
    {
        /* In-Place Records Management locks items by checking them 
         * out to the System Account. For our changes to persist 
         * for these items, the code updates the checked-in version. */
         
        postActions |=
                /* Discards the check-out to System Account before 
                 * updating the item. */
                ExpirationPostActions.UndoCheckOutBeforeUpdate |
                /* Checks the item back out to the System Account after 
                 * updating the item. */
                ExpirationPostActions.KeepCheckedOutToSystem;
    }

    return postActions;
}

若要更新锁定的项目,所有其他请求必须在提升范围内调用 BypassLocks(SPListItem, ByPassLockedItemDelegateMethod) 方法。

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.Office.RecordsManagement.RecordsRepository;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    Records.BypassLocks(myListItem, delegate(SPListItem item)
    {
        //Perform any action on the item.
    });
});

请参阅

概念

管理信息管理策略

管理记录和电子数据展示