When developing a custom Expiration formula in SharePoint you may want to pass in parameters/config data for the formula, e.g. only expire items where field X has value Y.
The method that you needs to implement for your formula has the below signature:
public DateTime? ComputeExpireDate(SPListItem item, XmlNode parametersData)
{ }
You can pass params/config data to your formula via the parametersData parameter. This is a piece of XML that will have the following schema:
<formula id="[the ID you assigned to your custom formula]"> <!--Your custom XML, e.g. the below --> <documentStatusFieldName>Status</documentStatusFieldName> <statusValueToExpire>Archived</statusValueToExpire> </formula>
You can set the XML contained within parametersData when you enable the Expiration policy feature for a policy using code. The code below for example enables the Expiration policy feature for a content type. The code configures the Expiration policy feature to use a custom Expiration formula, and pass in XML to be used inside that formula (via parametersData).
//Needs using Microsoft.Office.RecordsManagement.InformationPolicy; public void EnableExpirationForContentType(SPContentType contentType) { const string EXPIRATION_POLICY_FEATURE_ID = "Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration"; //Ensure that a policy is defined for the content type Policy policy = Policy.GetPolicy(contentType); if (policy == null) { Policy.CreatePolicy(contentType, null); policy = Policy.GetPolicy(contentType); } //Always overwrite any existing Expiration setting for the policy (i.e. delete if exists) if (policy.Items[EXPIRATION_POLICY_FEATURE_ID] != null) { policy.Items.Delete(EXPIRATION_POLICY_FEATURE_ID); } //The id attribute must points to an Expiration formula you have deployed to the server string policyFeatureXml = @”<data> <formula id=”"MyCustomExpirationFormula”"> <!—From here on is your custom XML—> <documentStatusFieldName>DocumentStatus</documentStatusFieldName> <documentStatusToExpire>Archived</documentStatusToExpire> </formula> <action type="workflow" id="[guid of a workflow that has been associated with the content type]" /> </data>"; //Enable the Expiration policy feature policy.Items.Add(EXPIRATION_POLICY_FEATURE_ID, policyFeatureXml); }
Note the id attribute in the XML must point to an Expiration formula you have deployed to the server. Follow this tutorial for how to develop, deploy and debug a custom Expiration formula: http://blah.winsmarts.com/2008-10-Authoring_custom_expiration_policies_and_actions_in_SharePoint_2007.aspx.
Great post! Thank you very much for covering this. From where do the parameters get passed? From the admin GUI where you set the Expiration policy?
Hey Daniel, OOTB there is no GUI to define parameters for your custom Expiration formula. The only way (that I know of) to define parameters and pass in values for them is in your custom code, as shown in this post. You should consider carefully when using this approach. This is because if a user edit the Information Management Policy through the OOTB GUI, then your custom parameters and their values will be overwritten by the GUI – hence lost.