Without going into too much detail, the compliance details page calculates the expiration date on the fly based on the ItemRetentionFormula for the item. The Expiration Date, however, is updated by an event receiver. This is why you have to run a SystemUpdate() on all documents after you enable retention policies.
I found that the event receivers were missing from these document libraries. How did this happen with a few of the documents having an Expiration Date? I don't know, but I do know how to add them back.
Luckily, adding the event receivers is very easy to do with a bit of code. For each of the event types you just have to run this code:
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate");
Here's the entire piece of code that creates a command line tool to update a library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.SharePoint; | |
namespace AddRMEventReceivers | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
String sUrl = args[0].ToString(); | |
string sList = args[1].ToString(); | |
using (SPWeb web = new SPSite(sUrl).OpenWeb()) | |
{ | |
SPList list = web.Lists[sList]; | |
String[] types = new String[] {"ItemAdded", "ItemUpdated", "ItemCheckedIn", "ItemUncheckedOut", "ItemFileMoved"}; | |
foreach (SPEventReceiverDefinition def in list.EventReceivers) | |
{ | |
types = types.Where(val => val != def.Type.ToString()).ToArray(); | |
for (int i = 0; i < types.Length; i++) | |
{ | |
Console.WriteLine(types[i].ToString()); | |
} | |
} | |
if (types.Length > 0) | |
{ | |
for (int i = 0; i < types.Length; i++) | |
{ | |
string sType = types[i].ToString(); | |
if (sType == "ItemAdded") | |
{ | |
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate"); | |
list.Update(); | |
Console.WriteLine(sType + " Added!"); | |
} | |
if (sType == "ItemUpdated") | |
{ | |
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate"); | |
list.Update(); | |
Console.WriteLine(sType + " Added!"); | |
} | |
if (sType == "ItemCheckedIn") | |
{ | |
list.EventReceivers.Add(SPEventReceiverType.ItemCheckedIn, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate"); | |
list.Update(); | |
Console.WriteLine(sType + " Added!"); | |
} | |
if (sType == "ItemUncheckedOut") | |
{ | |
list.EventReceivers.Add(SPEventReceiverType.ItemUncheckedOut, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate"); | |
list.Update(); | |
Console.WriteLine(sType + " Added!"); | |
} | |
if (sType == "ItemFileMoved") | |
{ | |
list.EventReceivers.Add(SPEventReceiverType.ItemFileMoved, "Microsoft.Office.Policy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.Office.RecordsManagement.Internal.UpdateExpireDate"); | |
list.Update(); | |
Console.WriteLine(sType + " Added!"); | |
} | |
} | |
web.AllowUnsafeUpdates = true; | |
web.Update(); | |
foreach (SPListItem item in list.Items) | |
{ | |
item.SystemUpdate(); | |
} | |
web.AllowUnsafeUpdates = false; | |
web.Update(); | |
} | |
} | |
} | |
} | |
} |
No comments:
Post a Comment