Introduction
Information Management policies enable you to control the specified action. You can set up a policy to control how to track, who can access and how long to retain documents. Each policy can establish a set of rules. This can be created in one of the following 3 ways:
- Multiple content types within a site collection
- Site content type
- List or Library
By default DateTime Expiration formulas are available in SharePoint. If we need to do some other calculations apart from DateTime, in such cases we will be creating a custom expiration formula. This article explains how to create a custom expiration policy in SharePoint 2013.
Prerequisites
- Ensure you have a SharePoint site.
Scenario
Moving the list items to the recycle bin when the status is set to Decline.
Use the following procedure
- Create a Custom list called "Test". I want to move the item to the recycle bin if the item status is set to "Decline". This article shows how to create a custom expiration formula in SharePoint 2013.
- Create a custom list called called Test.
- Add a field called "Status" and type to be "Choice field".
- Open Visual Studio 2012
Select "Start" -> "All Programs" -> "Microsoft Visual Studio 2010" then right-click on "Microsoft Visual Studio 2012" and click on "Run as administrator".
- Click on "File" -> "New" -> "Project...".
- In the Templates pane, expand the Visual C# node and then select the SharePoint Solutions.
- To create an Empty SharePoint Project, select SharePoint2013-Empty Project.
- Enter the solution name has "SharePoint.CustomExpirationPolicy".
- Click on "OK".
- Enter the site URL set the "Trust level" to Farm Solution and click on the Finish button.
- Now let's create a ExpirationPolicy class in the solution to do so.
- Click on "Add" -> "New" -> "Item" -> "Add a Class".
- Now add a Reference for "Microsoft.Office.Policy".
- Double-click on ExpirationPolicy.cs and add the following namespace:
using Microsoft.Office.RecordsManagement.PolicyFeatures;
- Inherit the IExpirationFromula.
- Override the ComputeExpireDate method.
- Replace the class with the following lines of code.
- public class ExpirationPolicy : IExpirationFormula
- {
- public Nullable<DateTime> ComputeExpireDate(SPListItem item, System.Xml.XmlNode parametersData)
- {
- DateTime? expirationStatus = null;
-
- if (item["Status"] != null && (!string.IsNullOrEmpty(item["Status"].ToString())))
- {
-
- if (item["Status"].ToString() == "Decline")
- expirationStatus = DateTime.Now;
- }
- return expirationStatus;
- }
-
- }
- Now its time to create a feature receiver.
- Right-click on "Feature" then select "Add Feature".
- Rename the feature to "Custom Expiration Feature".
- Right-click on "Custom Expiration Feature".
- Select "Add Event Receiver".
Double-click on the event receiver file.
Add the following lines of code inside the class.
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- string expirationFormulaID = "MyFirstCustomExpirationFormula";
- string expirationFormulaName = "My First Custom Expiration Formula";
- string expirationFormulaDesc = "My First Custom Expiration Formula";
- string xmlExpirationFormula = "<PolicyResource xmlns=\"urn:schemas-microsoft-com:office:server:policy\"" +
- " id = \"" + expirationFormulaID + "\"" +
- " featureId=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration\"" +
- " type = \"DateCalculator\"> <Name>" + (expirationFormulaName) + "</Name>" +
- "<Description>" + (expirationFormulaDesc) + "</Description>" +
- "<AssemblyName>SharePoint.CustomExpirationPolicy, Version=1.0.0.0, Culture=neutral," +
- "PublicKeyToken=2ae760fca3aac67d</AssemblyName>" +
- "<ClassName>SharePoint.CustomExpirationPolicy.ExpirationPolicy</ClassName>" +
- "</PolicyResource>";
- try
- {
- PolicyResourceCollection.Delete(expirationFormulaID);
- }
- catch (Exception ex)
- {
- }
- PolicyResource.ValidateManifest(xmlExpirationFormula);
-
- PolicyResourceCollection.Add(xmlExpirationFormula);
- }
- Right-click on the solution Build and then Deploy.
- That's It. Now let's start testing.
Testing
- Let's add the items in the Test list. One item contains the status Accept and the other one Decline.
- Now go to "List" -> "List Settings".
- Configure the Information management policy for this list.
- Click on "Information management policy settings".
- Click on the item.
- To enable the retention, check on the check box.
- Click on "Add a retention stage".
- A pop-up will be displayed where we can set our custom retention policy that we just created.
- Select the event as "Set by a custom retiontion formula installed on this server" where our custom ‘My First Custom Expiration Formula" will be displayed; select that.
- Now set the action to ‘Move to Recycle bin" and click on "OK".
- Now go to Central Admin, click on "Monitoring" -> "Review Job Definition".
- Click on ‘Expiration policy".
- Run the job.
- That's it!
- Now if you got to the List we will not be able to see the records that contain the status Decline.
- The item that contains the status decline will be moved to the Recycle Bin.
- Go to to "Top level SiteSettings" -> "Recycle bin".
Summary
Thus in this article you saw how to create a custom expiration policy in SharePoint 2013.