How to: Expire Actions with Post Actions and Bypass Locks in SharePoint Server 2010 (ECM)
Applies to: SharePoint Server 2010
Working properly with records requires that you work correctly with custom policy actions. Because the object model provides few warnings or errors if custom policy actions are registered incorrectly or used incorrectly, working with custom policy actions requires a deeper understanding of how the object model works.
This topic provides tips and code examples to help you use the object model to expire actions with post actions and bypass locks. Because retention policy is expected to work for records in all cases, we recommend that you write a custom action so that retention policy can manage a record even if the specific action that you are writing is not meant to handle records. This topic explains how to wrap a custom policy action in code that implements the OnExpirationWithPostActions(SPListItem, XmlNode, Time) method to work with records, and how to bypass locks on records if you do not want your code to process locked records.
If you use the new IExpirationActionWithPostActions interface and implement the OnExpirationWithPostActions(SPListItem, XmlNode, Time) method, return the correct ExpirationPostActions action to update the SPItem object if it is a record.
If you use the IExpirationAction interface or implement only the Expiration class, the Records Management solution in Microsoft SharePoint Server 2010 manages custom policy actions correctly by default.
Example
The ExpirationPostActions example demonstrates how to declare a post action that executes for an item upon its expiration. The code recalculates the item’s expiration date, updates the item, and logs an audit event for the action after a user-defined custom action is run.
The code also checks to see whether the In-Place Records Management feature has locked the item. The code uses post actions to discard records checked out to the System Account, updates the item, and then checks out the item to the System Account again.
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;
}
To update an item that is locked, all other requests must call the BypassLocks(SPListItem, ByPassLockedItemDelegateMethod) method in an elevated scope.
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.
});
});
See Also
Concepts
Managing Information Management Policy in SharePoint Server 2010 (ECM)
Developing with SharePoint Server 2010 Records and eDiscovery Features (ECM)