In SharePoint 2010, we can set an expiration date for documents so that when the expiration date is reached a specified action is performed.
IntroductionIn 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+_ AddDaysDateTime column+_ AddMonthsDateTime column+_ AddYearsSometimes 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:
Create a custom expiration formula:
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
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 DeployRight-click on the solution and then click on Deploy.Add the custom expiration formula to the list:
SummaryThus in this article we have seen how to create a custom expiration formula in SharePoint 2010.
Basic Operations On SharePoint Using PnP PowerShell Scripts