CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Retention policies

Ziegler provides a cool intro, implementation sample and much more.

When deployed we can apply this policy to a contenttype in the UI, or in code.

To create our own expiration logic we have to implement IExpirationFormula and its ComputeExpireDate:

public class TaskExpiration : IExpirationFormula
{
	public DateTime? ComputeExpireDate(SPListItem item,
						XmlNode parametersData)
	{
		if (!item["Status"].Equals("Completed"))
		{
			return null;
		}
		var dt = (DateTime) item["Modified"];
		return dt.AddDays(30);
	}
}

In order to see IExpirationFormula, add a reference to Microsoft.Office.Policy (and maybe Microsoft.Office.DocumentManagement):

To see our custom retention policy, we have to register it in xml, we can do it in Feature Receiver like Yaroslav:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    const string xmlManifest =
       "<PolicyResource xmlns='urn:schemas-microsoft-com:office:server:policy'" +
       " id = 'Takana.TaskRetentionPolicy'" +
       " featureId='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration'" +
       " type = 'DateCalculator'>" +
       " <Name>Takana Task Retention Policy</Name>" +
       "<Description>Tasks expire 30 days after they have been completed</Description>" +
       "<AssemblyName>Takana.SharePoint, Version=1.0.0.0, Culture=neutral, " +
       "PublicKeyToken=920c0327f8b01d97</AssemblyName>" +
       "<ClassName>Takana.SharePoint.Policies.TaskExpiration</ClassName>" +
       "</PolicyResource>";
    PolicyResourceCollection.Add(xmlManifest);
}

The feature has to be Webapplication scoped, otherwise you get “No access” (“Ingen tillgang” i nb-NO) when you try to add this xmlManifest.

Or in Powershell, like it is described by Microsoft. Create an xml like this:

<PolicyResource xmlns="urn:schemas-microsoft-com:office:server:policy" 
    id = "Takana.TaskRetentionPolicy" 
    featureId="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration" 
    type = "DateCalculator">
    <Name>Takana Task Retention Policy</Name>
    <Description>Tasks expire 30 days after they have been completed</Description>
    <AssemblyName>Takana.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=920c0327f8b01d97</AssemblyName>
    <ClassName>Takana.SharePoint.Policies.TaskExpiration</ClassName>
</PolicyResource>

Then access the xml file and add it to policy resource collection:

$policyResource = Get-Content .\takan.taskretentionpolicy.xml
[Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResource]::ValidateManifest($policyResource)
[Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection]::Add($policyResource)

If you want to remove your policy resource, just run delete with your resource id:

[Microsoft.Office.RecordsManagement.InformationPolicy.PolicyResourceCollection]::Delete("Takana.TaskRetentionPolicy")

The items are collected by the “Expiration policy” timer job:

Expiration Policy
Enumerates list items and looks for those with an expiration date that has already occurred. For those items, runs disposition processing. Disposition processing most often results in deleting items, but it can perform other actions, such as processing disposition workflows.

If you want to see the changes directly when developing, you can change the interval of timer job.

Expiration Policy timer job and SP1

If the timer job doesn’t run, re-activate RecordsManagement feature:

Install-SPFeature RecordsManagement -force
stsadm -o setpolicyschedule -schedule "daily at 00:10:00"

This solved the problem why the items are not deleted. But my custom policy resource had to be re-published, too.

Expiration Policy is the timer job which removes the items. What does the Information Management Policy timer job?

To associate a policy to a content type is easy in UI, but not so straight forward in code. Here is some beginning:

$web = get-spweb http://takana
$list = $web.Lists["Tasks"]
$ctype = $list.ContentTypes["Task"]
[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CanHavePolicy($ctype)
[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($ctype, $null)
$p = [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($c)
$data = Get-Content .\takana.taskretentionpolicy.schedule.xml
$p.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration", $data)
$site = get-spsite http://dev
[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::ProcessChanges($site)

This code loads the xml from the same folder:

<Schedules nextStageId="2">
    <Schedule type="Default">
        <stages>
            <data stageId="1" stageDeleted="true" />
                <formula id="Takana.TaskRetentionPolicy" />" +
                <action type="action" id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete" />
            </data>
        </stages>
    </Schedule>
</Schedules>

Or add to a list:

$web = get-spweb http://takana
$list = $web.Lists["Tasks"]
$policySettings = new-object Microsoft.Office.RecordsManagement.InformationPolicy.ListPolicySettings($list)

if ($policySettings.ListHasPolicy -neq $false)
{
	#make the list use a custom list policy
	$policySettings.UseListPolicy = true;
	$policySettings.Update();
}

The code to add policy to a c ontent type has not been tested yet.

Update 20120831: A nice description and code samples on Yaroslav Pentsarsky’s blog: Help users manage SharePoint content by setting expiration policies with PowerShell

Advertisements

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

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

Paul J. Swider - RealActivity

RealActivity is a specialized healthcare services and solution advisory firm.

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

BigData and Blockchain expert in Toronto

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

%d bloggers like this: