Setup custom retention policy with custom Expiration Formula for custom Document Content Types in individual webs.

Published May 01 2019 04:03 PM 187 Views
Microsoft

First published on TECHNET on Oct 16, 2016
This post is a contribution from Manish Joshi, an engineer with the SharePoint Developer Support team

Custom Formula is implemented with Class CustomFormula.cs, compiled to a custom assembly and consumed through the following method:
Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection.Add(xmlExpirationForm...


Following is sample code to install the custom formula


string strExpirationFormulaID = "testCustomExpirationFormula";
string strExpirationFormulaName = "testCustomExpirationFormula";
string strExpirationFormulaDesc = "testCustomExpirationFormula";

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>RetentionPolicy, Version=1.0.0.0, Culture=neutral," +
"PublicKeyToken=f3ef95b35506e557</AssemblyName>" +
"<ClassName>RetentionPolicy.CustomFormula</ClassName>" +
"</PolicyResource>";
try
{

PolicyResourceCollection.Delete(strExpirationFormulaID);
}
catch (Exception ex)
{

// ex.Message.ToString();
}

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




To set custom retention policy for a content type with above registered CustomExpirationFormula, the sample code is:



private static string GeneratePolicyCustomData(string _strExpirationFormulaID)
{
StringBuilder sb = new StringBuilder();
try
{
sb.AppendLine("<Schedules nextStageId='3'>");
sb.AppendLine("<Schedule type='Default'>");
sb.AppendLine("<stages>");

// Send Expiry Notification when Today = Near Expiry + 0 days
sb.AppendLine("<data stageId='1'>");
sb.AppendLine("<formula id='" + _strExpirationFormulaID + "'>");

sb.AppendLine("</formula>");
sb.AppendLine("<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin' />");
sb.AppendLine("</data>");

sb.AppendLine("</stages>");
sb.AppendLine("</Schedule>");
sb.AppendLine("</Schedules>");
}
catch (Exception ex)
{
throw ex;
}
return sb.ToString();
}

private void SetPolicy(string contentTypeName, string strExpirationFormulaID)
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = web.ContentTypes.Cast<SPContentType>().Where(cty => cty.Name.Trim().ToLower() == contentTypeName.ToLower()).FirstOrDefault();
if (contentType != null)
{
try
{
if (Policy.GetPolicy(contentType) != null)
{
Policy.DeletePolicy(contentType);
}

Policy.CreatePolicy(contentType, null);
Policy policy = Policy.GetPolicy(contentType);

string policyCustomData = GeneratePolicyCustomData(strExpirationFormulaID);

policy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration", policyCustomData);
contentType.Update();

}
catch (Exception ex)
{
throw ex;
}
}//EndOfIf

}
}

string strExpirationFormulaID = "testCustomExpirationFormula";
string customCT1 = "CustomDocument1";
SetPolicy(customCT1, strExpirationFormulaID);




To set the custom retention policy at the web's content type level, use following code to create a child content type for a web.
Be sure to use a different content type name for the root web's content type though.

For root web,


SPContentType sdFileContentType = new SPContentType(contentTypes["SecureDocFile"], contentTypes, "SecureDocFile_rootWeb");
sdFileContentType.Group = "CentRic Portal Content Types";
web.ContentTypes.Add(sdFileContentType);

SPContentType sdMessageContentType = new SPContentType(contentTypes["SecureDocMessage"], contentTypes, "SecureDocMessage_rootWeb");




For non-root web:


SPContentType sdFileContentType = new SPContentType(contentTypes["SecureDocFile"], contentTypes, "SecureDocFile_Web");
sdFileContentType.Group = "CentRic Portal Content Types";
web.ContentTypes.Add(sdFileContentType);

SPContentType sdMessageContentType = new SPContentType(contentTypes["SecureDocMessage"], contentTypes, "SecureDocMessage_Web");

%22%3B%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20PolicyResourceCollection.Delete(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20(Exception%20ex)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20ex.Message.ToString()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20PolicyResource.ValidateManifest(xmlExpirationFormula)%3B%20%3CBR%20%2F%3E%20PolicyResourceCollection.Add(xmlExpirationFormula)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20set%20custom%20retention%20policy%20for%20a%20content%20type%20with%20above%20registered%20CustomExpirationFormula%2C%20the%20sample%20code%20is%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%20private%20static%20string%20GeneratePolicyCustomData(string%20_strExpirationFormulaID)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20StringBuilder%20sb%20%3D%20new%20StringBuilder()%3B%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CSCHEDULES%20nextstageid%3D%22'3'%22%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CSCHEDULE%20type%3D%22'Default'%22%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CSTAGES%3E%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2F%2F%20Send%20Expiry%20Notification%20when%20Today%20%3D%20Near%20Expiry%20%2B%200%20days%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CDATA%20stageid%3D%22'1'%22%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CFORMULA%20id%3D%22'%26quot%3B%22%3E%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3C%2FFORMULA%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3CACTION%20type%3D%22'action'%22%20id%3D%22'Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin'%22%3E%3C%2FACTION%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3C%2FDATA%3E%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3C%2FSTAGES%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3C%2FSCHEDULE%3E%22)%3B%20%3CBR%20%2F%3E%20sb.AppendLine(%22%3C%2FSCHEDULES%3E%22)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20(Exception%20ex)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20throw%20ex%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20return%20sb.ToString()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20private%20void%20SetPolicy(string%20contentTypeName%2C%20string%20strExpirationFormulaID)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20using%20(SPSite%20site%20%3D%20new%20SPSite(SPContext.Current.Web.Url))%20%3CBR%20%2F%3E%20using%20(SPWeb%20web%20%3D%20site.OpenWeb())%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20SPContentType%20contentType%20%3D%20web.ContentTypes.Cast%3CSPCONTENTTYPE%3E().Where(cty%20%3D%26gt%3B%20cty.Name.Trim().ToLower()%20%3D%3D%20contentTypeName.ToLower()).FirstOrDefault()%3B%20%3CBR%20%2F%3E%20if%20(contentType%20!%3D%20null)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20if%20(Policy.GetPolicy(contentType)%20!%3D%20null)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Policy.DeletePolicy(contentType)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Policy.CreatePolicy(contentType%2C%20null)%3B%20%3CBR%20%2F%3E%20Policy%20policy%20%3D%20Policy.GetPolicy(contentType)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20policyCustomData%20%3D%20GeneratePolicyCustomData(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20policy.Items.Add(%22Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration%22%2C%20policyCustomData)%3B%20%3CBR%20%2F%3E%20contentType.Update()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20(Exception%20ex)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20throw%20ex%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%2F%2FEndOfIf%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20strExpirationFormulaID%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3E%20string%20customCT1%20%3D%20%22CustomDocument1%22%3B%20%3CBR%20%2F%3E%20SetPolicy(customCT1%2C%20strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FSPCONTENTTYPE%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20set%20the%20custom%20retention%20policy%20at%20the%20web's%20content%20type%20level%2C%20use%20following%20code%20to%20create%20a%20child%20content%20type%20for%20a%20web.%20%3CBR%20%2F%3E%20Be%20sure%20to%20use%20a%20different%20content%20type%20name%20for%20the%20root%20web's%20content%20type%20though.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20root%20web%2C%20%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%20SPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_rootWeb%22)%3B%20%3CBR%20%2F%3E%20sdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3E%20web.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20SPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_rootWeb%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20non-root%20web%3A%20%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%20SPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_Web%22)%3B%20%3CBR%20%2F%3E%20sdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3E%20web.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20SPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_Web%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%0A%20%0A%22%3B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicyResourceCollection.Delete(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20ex.Message.ToString()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicyResource.ValidateManifest(xmlExpirationFormula)%3B%20%3CBR%20%2F%3EPolicyResourceCollection.Add(xmlExpirationFormula)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20set%20custom%20retention%20policy%20for%20a%20content%20type%20with%20above%20registered%20CustomExpirationFormula%2C%20the%20sample%20code%20is%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Eprivate%20static%20string%20GeneratePolicyCustomData(string%20_strExpirationFormulaID)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EStringBuilder%20sb%20%3D%20new%20StringBuilder()%3B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSCHEDULES%20nextstageid%3D%22'3'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSCHEDULE%20type%3D%22'Default'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSTAGES%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20Send%20Expiry%20Notification%20when%20Today%20%3D%20Near%20Expiry%20%2B%200%20days%20%3CBR%20%2F%3Esb.AppendLine(%22%3CDATA%20stageid%3D%22'1'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CFORMULA%20id%3D%22'%26quot%3B%22%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FFORMULA%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CACTION%20type%3D%22'action'%22%20id%3D%22'Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin'%22%3E%3C%2FACTION%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FDATA%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSTAGES%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSCHEDULE%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSCHEDULES%3E%22)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ethrow%20ex%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ereturn%20sb.ToString()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Eprivate%20void%20SetPolicy(string%20contentTypeName%2C%20string%20strExpirationFormulaID)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eusing%20(SPSite%20site%20%3D%20new%20SPSite(SPContext.Current.Web.Url))%20%3CBR%20%2F%3Eusing%20(SPWeb%20web%20%3D%20site.OpenWeb())%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3ESPContentType%20contentType%20%3D%20web.ContentTypes.Cast%3CSPCONTENTTYPE%3E().Where(cty%20%3D%26gt%3B%20cty.Name.Trim().ToLower()%20%3D%3D%20contentTypeName.ToLower()).FirstOrDefault()%3B%20%3CBR%20%2F%3Eif%20(contentType%20!%3D%20null)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eif%20(Policy.GetPolicy(contentType)%20!%3D%20null)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EPolicy.DeletePolicy(contentType)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicy.CreatePolicy(contentType%2C%20null)%3B%20%3CBR%20%2F%3EPolicy%20policy%20%3D%20Policy.GetPolicy(contentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20policyCustomData%20%3D%20GeneratePolicyCustomData(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Epolicy.Items.Add(%22Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration%22%2C%20policyCustomData)%3B%20%3CBR%20%2F%3EcontentType.Update()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ethrow%20ex%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%2F%2FEndOfIf%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20strExpirationFormulaID%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3Estring%20customCT1%20%3D%20%22CustomDocument1%22%3B%20%3CBR%20%2F%3ESetPolicy(customCT1%2C%20strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPCONTENTTYPE%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20set%20the%20custom%20retention%20policy%20at%20the%20web's%20content%20type%20level%2C%20use%20following%20code%20to%20create%20a%20child%20content%20type%20for%20a%20web.%20%3CBR%20%2F%3EBe%20sure%20to%20use%20a%20different%20content%20type%20name%20for%20the%20root%20web's%20content%20type%20though.%20%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20root%20web%2C%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3ESPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_rootWeb%22)%3B%20%3CBR%20%2F%3EsdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3Eweb.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3ESPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_rootWeb%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20non-root%20web%3A%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3ESPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_Web%22)%3B%20%3CBR%20%2F%3EsdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3Eweb.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3ESPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_Web%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CLINGO-SUB%20id%3D%22lingo-sub-509984%22%20slang%3D%22en-US%22%3ESetup%20custom%20retention%20policy%20with%20custom%20Expiration%20Formula%20for%20custom%20Document%20Content%20Types%20in%20individual%20webs.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509984%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Oct%2016%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EThis%20post%20is%20a%20contribution%20from%20Manish%20Joshi%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%20%3CBR%20%2F%3E%3CBR%20%2F%3ECustom%20Formula%20is%20implemented%20with%20Class%20CustomFormula.cs%2C%20compiled%20to%20a%20custom%20assembly%20and%20consumed%20through%20the%20following%20method%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Foffice%2Fms567515.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection.Add(xmlExpirationFormula)%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EFollowing%20is%20sample%20code%20to%20install%20the%20custom%20formula%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Estring%20strExpirationFormulaID%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3Estring%20strExpirationFormulaName%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3Estring%20strExpirationFormulaDesc%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20xmlExpirationFormula%20%3D%20%22%3CPOLICYRESOURCE%20xmlns%3D%22%5C%26quot%3Burn%3Aschemas-microsoft-com%3Aoffice%3Aserver%3Apolicy%5C%26quot%3B%26quot%3B%22%3E%3C%2FPOLICYRESOURCE%3E%22%20id%20%3D%20%5C%22%22%20%2B%20strExpirationFormulaID%20%2B%20%22%5C%22%22%20%2B%20%3CBR%20%2F%3E%22%20featureId%3D%5C%22Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration%5C%22%22%20%2B%20%3CBR%20%2F%3E%22%20type%20%3D%20%5C%22DateCalculator%5C%22%26gt%3B%20%3CNAME%3E%22%20%2B%20(strExpirationFormulaName)%20%2B%20%22%3C%2FNAME%3E%22%20%2B%20%3CBR%20%2F%3E%22%3CDESCRIPTION%3E%22%20%2B%20(strExpirationFormulaDesc)%20%2B%20%22%3C%2FDESCRIPTION%3E%22%20%2B%20%3CBR%20%2F%3E%22%3CASSEMBLYNAME%3ERetentionPolicy%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%22%20%2B%20%3CBR%20%2F%3E%22PublicKeyToken%3Df3ef95b35506e557%3C%2FASSEMBLYNAME%3E%22%20%2B%20%3CBR%20%2F%3E%22%3CCLASSNAME%3ERetentionPolicy.CustomFormula%3C%2FCLASSNAME%3E%22%20%2B%20%3CBR%20%2F%3E%22%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%22%3B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicyResourceCollection.Delete(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20ex.Message.ToString()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicyResource.ValidateManifest(xmlExpirationFormula)%3B%20%3CBR%20%2F%3EPolicyResourceCollection.Add(xmlExpirationFormula)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20set%20custom%20retention%20policy%20for%20a%20content%20type%20with%20above%20registered%20CustomExpirationFormula%2C%20the%20sample%20code%20is%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Eprivate%20static%20string%20GeneratePolicyCustomData(string%20_strExpirationFormulaID)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EStringBuilder%20sb%20%3D%20new%20StringBuilder()%3B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSCHEDULES%20nextstageid%3D%22'3'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSCHEDULE%20type%3D%22'Default'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CSTAGES%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20Send%20Expiry%20Notification%20when%20Today%20%3D%20Near%20Expiry%20%2B%200%20days%20%3CBR%20%2F%3Esb.AppendLine(%22%3CDATA%20stageid%3D%22'1'%22%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CFORMULA%20id%3D%22'%26quot%3B%22%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FFORMULA%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3CACTION%20type%3D%22'action'%22%20id%3D%22'Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin'%22%3E%3C%2FACTION%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FDATA%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSTAGES%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSCHEDULE%3E%22)%3B%20%3CBR%20%2F%3Esb.AppendLine(%22%3C%2FSCHEDULES%3E%22)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ethrow%20ex%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ereturn%20sb.ToString()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Eprivate%20void%20SetPolicy(string%20contentTypeName%2C%20string%20strExpirationFormulaID)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eusing%20(SPSite%20site%20%3D%20new%20SPSite(SPContext.Current.Web.Url))%20%3CBR%20%2F%3Eusing%20(SPWeb%20web%20%3D%20site.OpenWeb())%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3ESPContentType%20contentType%20%3D%20web.ContentTypes.Cast%3CSPCONTENTTYPE%3E().Where(cty%20%3D%26gt%3B%20cty.Name.Trim().ToLower()%20%3D%3D%20contentTypeName.ToLower()).FirstOrDefault()%3B%20%3CBR%20%2F%3Eif%20(contentType%20!%3D%20null)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eif%20(Policy.GetPolicy(contentType)%20!%3D%20null)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EPolicy.DeletePolicy(contentType)%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3EPolicy.CreatePolicy(contentType%2C%20null)%3B%20%3CBR%20%2F%3EPolicy%20policy%20%3D%20Policy.GetPolicy(contentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20policyCustomData%20%3D%20GeneratePolicyCustomData(strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Epolicy.Items.Add(%22Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration%22%2C%20policyCustomData)%3B%20%3CBR%20%2F%3EcontentType.Update()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Ecatch%20(Exception%20ex)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Ethrow%20ex%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%2F%2FEndOfIf%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20strExpirationFormulaID%20%3D%20%22testCustomExpirationFormula%22%3B%20%3CBR%20%2F%3Estring%20customCT1%20%3D%20%22CustomDocument1%22%3B%20%3CBR%20%2F%3ESetPolicy(customCT1%2C%20strExpirationFormulaID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPCONTENTTYPE%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20set%20the%20custom%20retention%20policy%20at%20the%20web's%20content%20type%20level%2C%20use%20following%20code%20to%20create%20a%20child%20content%20type%20for%20a%20web.%20%3CBR%20%2F%3EBe%20sure%20to%20use%20a%20different%20content%20type%20name%20for%20the%20root%20web's%20content%20type%20though.%20%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20root%20web%2C%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3ESPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_rootWeb%22)%3B%20%3CBR%20%2F%3EsdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3Eweb.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3ESPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_rootWeb%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20non-root%20web%3A%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3ESPContentType%20sdFileContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocFile%22%5D%2C%20contentTypes%2C%20%22SecureDocFile_Web%22)%3B%20%3CBR%20%2F%3EsdFileContentType.Group%20%3D%20%22CentRic%20Portal%20Content%20Types%22%3B%20%3CBR%20%2F%3Eweb.ContentTypes.Add(sdFileContentType)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3ESPContentType%20sdMessageContentType%20%3D%20new%20SPContentType(contentTypes%5B%22SecureDocMessage%22%5D%2C%20contentTypes%2C%20%22SecureDocMessage_Web%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509984%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Oct%2016%2C%202016%20This%20post%20is%20a%20contribution%20from%20Manish%20Joshi%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20teamCustom%20Formula%20is%20implemented%20with%20Class%20CustomFormula.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-509984%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Sep 02 2020 04:12 PM
Updated by: