Pages

Search This Blog

Saturday, November 19, 2011

Create Custom Expiration Formula for Retention Policy


Sometimes we have a requirement where we need to enable Retention policy on document library. It provided by the SharePoint. We need to go to Library Setting -> Information management policy settings -> Enable Retention Policy and set the Retention Stage for expiration. Now if client have some custom formula for expiring the document then SharePoint proivde this function also but to achieve this functionality we need to deploy custom expiration formula on the server. Below is the code and steps to deploy Custom Expiration formula on the server.

1. Create new SharePoint Project and add reference of "Microsoft.Office.Policy.dll" from "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI"

2. Add below class for custom expiration formula


//Inherit your class from IExpirationFormula for SharePoint custom retention expiration formula
    public class CustomExpirationFormula : IExpirationFormula
    {
        public Nullable<DateTime> ComputeExpireDate(SPListItem item, System.Xml.XmlNode parametersData)
        {
            try
            {
                Nullable<DateTime> dtReturn = null;
                // add your custom custom logic for expiration
                if (item.Level == SPFileLevel.Published)
                {
                    if (!String.IsNullOrEmpty(Convert.ToString(item["CustomDateField"])))
                    {
                        TimeSpan diffTime = Convert.ToDateTime(item["CustomDateField"]).Subtract(DateTime.Now);
                        if (diffTime.Days <= 0)
                        {
                            //return current datetime for expire the current item
                            dtReturn = DateTime.Now;
                        }
                    }
                }
                return dtReturn;
            }
            catch
            {
                return null;
            }
        }
    }




3. Create new feature and add below code in FeatureReceiver to add custom formula in Site Collection


public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            String sExpirationID= "CustomExpirationFormula";
            String sFormulaName = "Custom Expiration Formula";
            String sFormulaDescription = "This formula is used to calculate date for retention";
                string xmlManifest = "<PolicyResource xmlns=\"urn:schemas-microsoft-com:office:server:policy\"" +
                " id = \"" + sExpirationID + "\"" +
                " featureId=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration\"" +
                " type = \"DateCalculator\">   <Name>" + (sFormulaName) + "</Name>" +
                "<Description>" + (sFormulaDescription) + "</Description>" +
                "<AssemblyName>CustomExpirationFormula, Version=1.0.0.0, Culture=neutral," +
                "PublicKeyToken=1a103f9011d7a733</AssemblyName>" +
                "<ClassName>CustomExpirationFormula.CustomExpirationFormula</ClassName>" +
                "</PolicyResource>";

                try
                {
                    PolicyResourceCollection.Delete(sExpirationID);
                }
                catch (Exception ex)
                {
                }
                PolicyResourceCollection.Add(xmlManifest);
         }


Deploy your solution and activate your feature. You can able to see you customexpiration formula in dropdown as shown in below screen shot





Happy Coding  !!!!!!!

1 comment: