Creating a Custom Expiration Formula Based On Metadata in SharePoint Server 2007 (Part 1 of 2)

Summary: Learn how to extend the existing expiration policy feature in Microsoft Office SharePoint Server 2007 by creating a policy resource to implement a custom expiration formula.

Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office SharePoint Server 2007

Microsoft Corporation

April 2008


SharePoint Products and Technologies provide an extensible framework for implementing and extending policy features for information management policies. The existing policy features in Microsoft Office SharePoint Server 2007 provide functionality for document bar codes, document labels, auditing, and expiration. In this Microsoft Office Visual How To, you learn how you can extend the existing expiration policy feature by creating a policy resource to implement a custom expiration formula.

Code It

The first step in creating a custom expiration formula is to reference the Microsoft.SharePoint and Microsoft.Office.Policy assemblies in your project, as shown in the following code example. The Microsoft.Office.Policy assembly provides access to the Microsoft.Office.RecordsManagement namespace and the Microsoft.Office.RecordsManagement.Policy namespace.

The next step is to create a class that derives from the IExpirationFormula interface in the Microsoft.Office.RecordsManagement.Policy namespace. In this class, you need one public method named ComputeExpireDate. This method accepts an SPListItem object and an XMLNode object, and it returns a nullable DateTime object. This class is invoked when the custom formula is executed. In this example, the logic is very simple and returns a DateTime object set to Now. This logic is extended to meet a specific scenario in Creating a Custom Expiration Formula Based On Metadata in SharePoint Server 2007 (Part 2 of 2).

To make this class available for use in Office SharePoint Server 2007, you must compile it with a strong name and put in a location from which it can be accessed, such as in the global assembly cache or in the _app_bin directory of the Office SharePoint Server Web application.

To prepare the custom formula class so it can be used by the expiration policy feature, you must describe it as a policy resource and add it to the collection of available policy resources. In this example, you use a PowerShell script to perform these actions. The Microsoft.Office.Policy assembly is used and is loaded by the script. Next, you create an XML string to describe the policy resource. The PolicyResource element contains attributes for the following:

  • Id   Fully qualified name of our custom expiration formula policy resource

  • featureId   Fully qualified name of the expiration policy feature

  • type   The resource type for the expiration formula policy resource

The XML string contains information for the name and description of the policy resource, and also information about the assembly and class. Before you add this policy resource to the collection, you validate the PolicyResource xml string by using the ValidateManifest method in the PolicyResource class. You then add the policy resource to the collection of policy resources by using the Add method of the PolicyResourceCollection class.

[System.Reflection.Assembly]::Load("Microsoft.Office.Policy, Version=, `
Culture=neutral, PublicKeyToken=71e9bce111e9429c")
$policyResource = "<PolicyResource xmlns='urn:schemas-microsoft-com:office:server:policy' "
$policyResource += "  id = 'CustomExpiration.CustomExpirationFormula' "
$policyResource += "  `
featureId='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration' "
$policyResource += "  type = 'DateCalculator'>"
$policyResource += "<Name>Expire Now</Name>"
$policyResource += "<Description>Custom Expiration Calculation</Description>"
$policyResource += "<AssemblyName>CustomExpiration, Version=, Culture=Neutral, `
$policyResource += `
$policyResource +="</PolicyResource>";

The result is that your expiration formula is made available to the expiration policy feature as a policy resource and is visible to users when creating a new expiration policy on a list of content types.

Figure 1. Custom expiration for new policy

Custom expiration for new policy

The final step is to test things to make sure the custom expiration formula is properly registered and can be invoked. To do this, create an expiration policy on a library, as follows.

To create an expiration policy on a library

  1. In a document library, click Settings, and then click Document Library Settings.

  2. Under Permissions and Management, click Information Management Policy Settings.

  3. Select Define a policy, and then click OK.

  4. On the Edit Policy page, select the Enable Expiration check box.

  5. Select the Set by a custom retention formula installed in this server option, and select the name of the custom expiration formula created in the example: Expire Now.

  6. Select the Perform this action option, and then click Delete.

  7. Click OK.

  8. Next, set a breakpoint within the ComputeExpireDate method, and upload a document to the library.

    This invokes the ComputeExpireDate method and hits the breakpoint. If this occurs, you have successfully set up the expiration formula, and you can now extend it with specific formula logic.

Read It

The expiration policy feature is part of the information management policy functionality provided by Microsoft Office SharePoint Server 2007. The expiration policy feature defines rules for how long content is retained and the action that occurs when the retention period expires. This Microsoft Office Visual How To shows how to extend the expiration formula by creating a policy resource and making it available to the expiration policy feature. In addition, you can also extend the expiration action.

An alternative approach to the custom expiration formula and action is to create a timer job to perform the calculation and action. However, implementing a timer job that evaluates and takes action on every item is not an easy development task. By using a custom formula, you can take advantage of the information-management policy infrastructure, which has already solved this problem.

This example shows how to use a PowerShell script to describe the functionality as a policy resource, and then add the resource to the collection of policy resources that are available in the records center. You can also remove the resource from the policy resource collection in a PowerShell script by calling the Delete method of the PolicyResourceCollection class, passing in the fully qualified class name of the policy resource, as shown here.

[System.Reflection.Assembly]::Load("Microsoft.Office.Policy, Version=, Culture=neutral, `
$id = "CustomExpiration.CustomExpirationFormula"

Although PowerShell scripts are used here, you should use best practices when deciding how to deploy this functionality to an environment outside of a single development computer. You can convert the functionality in these scripts to Microsoft Visual C# or Microsoft Visual Basic and use it in callout code of a SharePoint Feature.

See It Custom Expiration Formula Based on Metadata

Watch the Video

Video Length: 00:06:04

File Size: 4.68 MB WMV

Explore It