How to Create Custom Expiration Formula in SharePoint 2010

Introduction

In SharePoint 2010, we can set an expiration date for documents so that when the expiration date is reached a specified action is performed. This is available in Information Management policy in library or list settings. This expiration policy can be set for content types or for the library/list. In some real scenarios you will not be able to use the default expiration formula. By default the following expiration formulas are provided by SharePoint:

DateTime column+_ AddDays
DateTime column+_ AddMonths
DateTime column+_ AddYears

Sometimes we may need to do some calculations based on some DateTime columns where we will not be able to use the default expiration formula. In such cases we might need to create a custom expiration formula. I have a custom list with "Title" and "EndDate" columns. I want to move the item to the recycle bin 12 hours after the EndDate value. In this article you will see how to create a custom expiration formula in SharePoint 2010.

Create an Empty SharePoint Project:

  1. Open Visual Studio 2010 by going to Start | All Programs | Microsoft Visual Studio 2010 | Right-click on Microsoft Visual Studio 2010 and click on Run as administrator.
  2. Go to the File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the SharePoint 2010 node.
  4. In the Templates pane, select Empty SharePoint Project.
  5. Enter the Name as CustomExpirationFormula and then click OK.

    Image1.jpg
     
  6. Enter the local site URL for debugging, select "Deploy as a farm solution" as in the following and then click on Finish.

    Image2.jpg

Create a custom expiration formula:

  1. In the Solution Explorer, right-click on the solution and then click on Add new item.
  2. In the Templates pane, select Class.

    Image3.jpg
     
  3. Enter the Name as ExpirationFormula.cs and then click on Add.
  4. In the Solution Explorer, right-click on the References folder and click on Add Reference.
  5. Add the following reference:

    1. Microsoft.Office.Policy.dll
     
  6. Double-click on ExpirationFormula.cs and add the following namespaces.

    1. using Microsoft.Office.RecordsManagement.PolicyFeatures;
     
  7. Replace ExpirationFormula.cs with the following code:
     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.RecordsManagement.PolicyFeatures;
    using
    Microsoft.SharePoint;

    namespace CustomExpirationFormula
    {
        public classExpirationFormula :IExpirationFormula
        {
            public Nullable<DateTime> ComputeExpireDate(SPListItem item, System.Xml.XmlNode parametersData)
            {
                DateTime? expirationDate =null;
                //// Make sure the enddate field has a value.
                if (item["EndDate"] != null && (!string.IsNullOrEmpty(item["EndDate"].ToString())))
                {
                    //// Get the enddate.
                    DateTime? endDate = Convert.ToDateTime(item["EndDate"].ToString());
                    expirationDate = endDate.Value.AddHours(12);
                }
                return expirationDate;
            }
        }
    }


Add feature event receiver

  1. In the Solution Explorer, right-click on the Feature folder and click on Add Feature.

    Image4.jpg
     
  2. Right-click on Feature1 and then click on Add Event Receiver.

    Image5.jpg
     
  3. Double-click on the Event Receiver cs file and replace with the following code:

    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using
    Microsoft.Office.RecordsManagement.InformationPolicy;

    namespace CustomExpirationFormula.Features.Feature1
    {
        /// <summary>
        /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
        ///</summary>
        ///<remarks>

        ///</remarks>

        [Guid("7b9dbb00-c0ee-4be2-ba26-3e1371b1f80a")]
        public classFeature1EventReceiver :SPFeatureReceiver
        {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                string strExpirationFormulaID ="VijaiCustomExpirationFormula";
                string strExpirationFormulaName = "Vijai Custom Expiration Formula";
                string strExpirationFormulaDesc = "Vijai Custom Expiration Formula";

               string xmlExpirationFormula ="<PolicyResource xmlns=\"urn:schemas-microsoft-com:office:server:policy\"" +
                                                 " id = \"" + strExpirationFormulaID + "\"" +
                                                 " featureId=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration\"" +
                                                 " type = \"DateCalculator\">   <Name>" + (strExpirationFormulaName) +"</Name>" +
                                                 "<Description>" + (strExpirationFormulaDesc) +"</Description>" +
                                                 "<AssemblyName>CustomExpirationFormula, Version=1.0.0.0, Culture=neutral," +
                                                 "PublicKeyToken=0d25f994eacb549f</AssemblyName>" +
                                                 "<ClassName>CustomExpirationFormula.ExpirationFormula</ClassName>" +
                                                 "</PolicyResource>";
                try
                {
                    PolicyResourceCollection.Delete(strExpirationFormulaID);            
                }
                catch (Exception ex)
                {
     
                }

               PolicyResource.ValidateManifest(xmlExpirationFormula);
                PolicyResourceCollection.Add(xmlExpirationFormula);    
            }
        }
    }

Build and Deploy

Right-click on the solution and then click on Deploy.

Add the custom expiration formula to the list:

  1. Go to list, in the ribbon interface click on List Settings.
  2. Click on Information management policy settings under Permissions and Management.

    Image6.jpg
     
  3. Click on Item content type under Content Type Policies.
  4. Select Enable Retention and then click on Add a retention stage.

    Image7.jpg
     
  5. Select the custom expiration formula under the Event section as shown in the following:

    Image8.jpg
     
  6. Select Move to Recycle Bin under the Action section.
  7. Click on Ok.

    Image9.jpg

Summary

Thus in this article we have seen how to create a custom expiration formula in SharePoint 2010.