Custom Expiration Policy in SharePoint 2013

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

  1. 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

  1. 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.
  2. Create a custom list called called Test.

    Custom List

  3. Add a field called "Status" and type to be "Choice field".
    Choice filed
  4. 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".

  5. Click on "File" -> "New" -> "Project...".

    Project
     
  6. In the Templates pane, expand the Visual C# node and then select the SharePoint Solutions.
  7. To create an Empty SharePoint Project, select SharePoint2013-Empty Project.
  8. Enter the solution name has "SharePoint.CustomExpirationPolicy".
  9. Click on "OK".

    OK

  10. Enter the site URL set the "Trust level" to Farm Solution and click on the Finish button.

    Trust level

  11. Now let's create a ExpirationPolicy class in the solution to do so.
  12. Click on "Add" -> "New" -> "Item" -> "Add a Class".

    ExpirationPolicy

  13. Now add a Reference for "Microsoft.Office.Policy".
  14. Double-click on ExpirationPolicy.cs and add the following namespace:

    using Microsoft.Office.RecordsManagement.PolicyFeatures;
     
  15. Inherit the IExpirationFromula.
  16. Override the ComputeExpireDate method.
  17. Replace the class with the following lines of code.
    1. public class ExpirationPolicy : IExpirationFormula  
    2. {  
    3.     public Nullable<DateTime> ComputeExpireDate(SPListItem item, System.Xml.XmlNode parametersData)  
    4.     {  
    5.         DateTime? expirationStatus = null;  
    6.         //// Make sure the status field has a value.  
    7.         if (item["Status"] != null && (!string.IsNullOrEmpty(item["Status"].ToString())))  
    8.         {  
    9.             //// Get the status.  
    10.             if (item["Status"].ToString() == "Decline")  
    11.                 expirationStatus = DateTime.Now;  
    12.         }  
    13.         return expirationStatus;  
    14.     }  
    15.   

  18. Now its time to create a feature receiver.
  19. Right-click on "Feature" then select "Add Feature".

    Add Feature

  20. Rename the feature to "Custom Expiration Feature".
  21. Right-click on "Custom Expiration Feature".
  22. Select "Add Event Receiver".

    Event Receiver

    Double-click on the event receiver file.

    Add the following lines of code inside the class.
    1. public override void FeatureActivated(SPFeatureReceiverProperties properties)  
    2. {  
    3.      string expirationFormulaID = "MyFirstCustomExpirationFormula";  
    4.      string expirationFormulaName = "My First Custom Expiration Formula";  
    5.      string expirationFormulaDesc = "My First Custom Expiration Formula";   
    6.      string xmlExpirationFormula = "<PolicyResource xmlns=\"urn:schemas-microsoft-com:office:server:policy\"" +  
    7.            " id = \"" + expirationFormulaID + "\"" +  
    8.            " featureId=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration\"" +  
    9.            " type = \"DateCalculator\">   <Name>" + (expirationFormulaName) + "</Name>" +  
    10.            "<Description>" + (expirationFormulaDesc) + "</Description>" +  
    11.            "<AssemblyName>SharePoint.CustomExpirationPolicy, Version=1.0.0.0, Culture=neutral," +  
    12.            "PublicKeyToken=2ae760fca3aac67d</AssemblyName>" +  
    13.            "<ClassName>SharePoint.CustomExpirationPolicy.ExpirationPolicy</ClassName>" +  
    14.            "</PolicyResource>";  
    15.     try  
    16.     {  
    17.         PolicyResourceCollection.Delete(expirationFormulaID);  
    18.     }  
    19.     catch (Exception ex)  
    20.     {   
    21.     }   
    22.     PolicyResource.ValidateManifest(xmlExpirationFormula);  
    23.     //add the xml to policy resource collection  
    24.     PolicyResourceCollection.Add(xmlExpirationFormula);  
  23. Right-click on the solution Build and then Deploy.
  24. That's It. Now let's start testing.

Testing

  1. Let's add the items in the Test list. One item contains the status Accept and the other one Decline.

    status

  2. Now go to "List" -> "List Settings".
  3. Configure the Information management policy for this list.

    list

  4. Click on "Information management policy settings".
  5. Click on the item.

    item
  6. To enable the retention, check on the check box.

    check box

  7. Click on "Add a retention stage".

    Add a retention stage
  8. A pop-up will be displayed where we can set our custom retention policy that we just created.
  9. 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.
  10. Now set the action to ‘Move to Recycle bin" and click on "OK".

    Move to Recycle bin

  11. Now go to Central Admin, click on "Monitoring" -> "Review Job Definition".

    Review Job Definition

  12. Click on ‘Expiration policy".

    Expiration policy

    Policy

  13. Run the job.
  14. That's it!
  15. Now if you got to the List we will not be able to see the records that contain the status Decline.

    records
  16. The item that contains the status decline will be moved to the Recycle Bin.
  17. Go to to "Top level SiteSettings" -> "Recycle bin".

    Recycle bin

Summary

Thus in this article you saw how to create a custom expiration policy in SharePoint 2013.