Retention policies
By Anatoly Mironov
Ziegler provides a cool intro, implementation sample and much more. When deployed we can apply this policy to a contenttype in the UI, or in code. To create our own expiration logic we have to implement IExpirationFormula and its ComputeExpireDate:
public class TaskExpiration : IExpirationFormula
{
public DateTime? ComputeExpireDate(SPListItem item,
XmlNode parametersData)
{
if (!item\["Status"\].Equals("Completed"))
{
return null;
}
var dt = (DateTime) item\["Modified"\];
return dt.AddDays(30);
}
}
```In order to see IExpirationFormula, add a reference to Microsoft.Office.Policy (and maybe Microsoft.Office.DocumentManagement): [![](https://sharepointkunskap.files.wordpress.com/2011/12/policy-dll.png "policy-dll")](https://sharepointkunskap.files.wordpress.com/2011/12/policy-dll.png) To see our custom retention policy, we have to register it in xml, we can do it in Feature Receiver like [Yaroslav](http://www.sharemuch.com/2011/01/10/creation-custom-retention-policies-for-sharepoint-2010-libraries/):
public override void FeatureActivated(SPFeatureReceiverProperties properties) { const string xmlManifest = “<PolicyResource xmlns=‘urn:schemas-microsoft-com:office:server:policy’” + " id = ‘Takana.TaskRetentionPolicy’" + " featureId=‘Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration’" + " type = ‘DateCalculator’>" + " Takana Task Retention Policy" + “Tasks expire 30 days after they have been completed” + “Takana.SharePoint, Version=1.0.0.0, Culture=neutral, " + “PublicKeyToken=920c0327f8b01d97” + “Takana.SharePoint.Policies.TaskExpiration” + “”; PolicyResourceCollection.Add(xmlManifest); }
<PolicyResource xmlns=“urn:schemas-microsoft-com:office:server:policy” id = “Takana.TaskRetentionPolicy” featureId=“Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration” type = “DateCalculator”> Takana Task Retention Policy Tasks expire 30 days after they have been completed Takana.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=920c0327f8b01d97 Takana.SharePoint.Policies.TaskExpiration
$policyResource = Get-Content .\takan.taskretentionpolicy.xml [Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResource]::ValidateManifest($policyResource) [Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection]::Add($policyResource)
[Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection]::Delete(“Takana.TaskRetentionPolicy”)
[The items are collected by the "Expiration policy" timer job](http://www.petestilgoe.com/2010/09/sharepoint-2010-timer-jobs-their-functions/ "See PeteStilGoe's blogg about all the timer jobs"):
> Expiration Policy Enumerates list items and looks for those with an expiration date that has already occurred. For those items, runs disposition processing. Disposition processing most often results in deleting items, but it can perform other actions, such as processing disposition workflows.
If you want to see the changes directly when developing, [you can change the interval of timer job](http://72.15.222.75/2008-10-Authoring_custom_expiration_policies_and_actions_in_SharePoint_2007.aspx).
#### Expiration Policy timer job and SP1
If the timer job doesn't run, [re-activate RecordsManagement feature](http://vspug.com/teameli/2008/10/13/record-center-information-management-policy-jobs-not-running/):
Install-SPFeature RecordsManagement -force stsadm -o setpolicyschedule -schedule “daily at 00:10:00”
$web = get-spweb http://takana $list = $web.Lists[“Tasks”] $ctype = $list.ContentTypes[“Task”] [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CanHavePolicy($ctype) [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($ctype, $null) $p = [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($c) $data = Get-Content .\takana.taskretentionpolicy.schedule.xml $p.Items.Add(“Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration”, $data) $site = get-spsite http://dev [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::ProcessChanges($site)
if ($policySettings.ListHasPolicy -neq $false) { #make the list use a custom list policy $policySettings.UseListPolicy = true; $policySettings.Update(); }