After searching on the for a way to getSharePoint to expire items on a list, that contains log items, i found a solution that could help me in the correct direction. By browsing through the MSDN i have made the following solution to programmatically set the expire date of an SPListItem in a SPList.
A Policy must be connected to the contenttype, that is setup on the list. So you must get the right contenttype, and attach the expire policy to it.
The variable loglist is a SPList control. the variable contenttypeid is a SPContentTypeID control. Also remember to rerference the Microsoft.Office.Policy dll.
//Const in my class
private const string ExpirationPolicyFeatureId = "Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration";
//Code snippet from my class
var contenttype = loglist.ContentTypes[contenttypeid];
var policy = Policy.GetPolicy(contenttype);
if (policy == null)
{
Policy.CreatePolicy(contenttype, null);
policy = Policy.GetPolicy(contenttype);
}
if (policy.Items[ExpirationPolicyFeatureId] == null)
{
const string policyData = "<data>"
+ "<formula id=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn\">"
+ "<number>7</number>"
+ "<property>Created</property>"
+ "<period>days</period>"
+ "</formula>"
+ "<action type=\"action\" id=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete\" />"
+ "</data>";
policy.Items.Add(ExpirationPolicyFeatureId, policyData);
}
You can choose from different actions. See all posible actions here: http://msdn.microsoft.com/en-us/library/dd928385(v=office.12).aspx
The code is tested and works with SharePoint 2007. But should work on SharePoint 2010 without any changes.
If you get a problem with the code like this error: “The object has been updated by another user since it was last fetched.” Then recreate the list control like this, and get the contenttype from the templist variable.:
var tempList = loglist.ParentWeb.Lists[loglist.ID]; var contenttype = tempList.ContentTypes[contenttypeid];
This code replaces line 5 in first code list.
Inspiration for this code have i found in the following articles:
- http://www.shareesblog.com/?p=195
- http://msdn.microsoft.com/en-us/library/dd928385(v=office.12).aspx