SharePoint Tech Blog

15/08/2009

Set expiry policy on SharePoint list using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 1:51 pm

MOSS out of box functionaliy allows creating information management policies on lists to expire content on those lists. If this needs to be done using code then the following code could be used to set the expiry policy programmatically using PowerShell.
This code requires the SharePoint Powershell Profile to be loaded beforehand


#Include the additional dll references required
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System")
[Void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Workflow")

$web=Get-SPWeb -webUrl "http://mossdev";

$list=$web.Lists["MyList"];

$listctype=$list.ContentTypes["Item"];

$policy=[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($listctype);

if ($policy -eq $null) {
$policy=[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]:: CreatePolicy($listctype,$null);
}

$policy.Description="Expire items the day after the expiry date";
$policy.Statement="Expire items the day after the expiry date";
$policy.Update();

$expirypolicyexists=$false;
if ($policy.Items.Count -ne 0)
{
foreach ($policyitem in $policy.Items)
{
if ($policyitem.Name -eq "Expiration")
{
$expirypolicyexists=$true;
}
}
}

if ($expirypolicyexists -eq $false)
{
$policyFeatureID=[Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration];
#set the internal field name and workflow id according to your needs
#in this case it has been set to expire the item based on the
#expiry date custom field and execute the disposition approval workflow
#when the item expires
formula
$policy.Items.Add($policyFeatureID,$customData);
$policy.Update();
"Expiry policy added"
}
else
{
"An expiry policy already exists - not overwriting";
}

$web.Dispose();

Advertisements

2 Comments »

  1. Where do I get this library

    [Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration]

    for use in PowerShell?

    Comment by David Hancock — 09/12/2010 @ 6:15 pm

    • If you use a Powershell Editor such as PowerGUI, you should get intellisense when you type in:

      [Microsoft.

      … the rest should follow.

      Comment by kctnpblog — 17/03/2011 @ 10:42 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: