Tuesday, September 16, 2014

Sharepoint create custom expiration Formula for retention policy programatically

This amazing blog helped me to create a custom expiration formula programatically

http://www.sharemuch.com/2011/01/10/creation-custom-retention-policies-for-sharepoint-2010-libraries/

http://blog.techperspect.com/2011/11/create-custom-expiration-formula-for.html

As defined in the blog you need to override the "IExpirationFormula" . be careful about the class to make public .

This class will define your custom expiration formula.The method implemented will return a datatype .You can write your logic for string columns as well as I did below

namespace Thi_CustomExp
{
    public class CustThiExpPolicy : IExpirationFormula
    {
       public Nullable<DateTime> ComputeExpireDate(SPListItem item, System.Xml.XmlNode parametersData)
       {
           try
           {
               Nullable<DateTime> dtReturn = null;
               // add your custom custom logic for expiration
              
               if (!String.IsNullOrEmpty(Convert.ToString(item["ThiloshNo"])))
               {
                   string a = Convert.ToString(item["ThiloshNo"]);
                   if (a == "1")
                   {
                       //return current datetime for expire the current item
                       dtReturn = DateTime.Now;
                   }
               }
               
               return dtReturn;
           }
           catch
           {
               return null;
           }
       }
    }
}

the return value will set the expiration date .

As defined in the blog the class will be referenced in a feature receiver.the feature can be deployed at web application or site collection scope.But I seemed to find difficulties in updating the feature once it is deployed.Like the logic in cs files wasnt getting updated.So if any changes I would recommend to create a expiration formula with different name.

No comments:

Post a Comment