Tuesday, 3 January 2012

Retention Policy for Sharepoint Foundation 2010

There are no Retention Policies available for Sharepoint Foundation 2010 but you can get around this if you are a little bit creative. I have created a Powershell script that will delete files from a library that have not been modified within a specified time.
In our case we wanted to setup a temporary library for files that would be deleted if they had not been modified in the past three months.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$SPWeb = Get-SPWeb http://yourSiteURL/
$list=$SPWeb.Lists["Documents Library Name"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count

$ThreeMonthsAgo = ((get-date).AddMonths(-3))

for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
    if([System.DateTime]$listItems[$x]["Modified"] -lt $ThreeMonthsAgo)
    {
        $listItems[$x].Delete()
    }
}
 
This script will delete the files but you could send them to the Recyle Bin instead using

$listItems[$x].Recycle()
 
You can change the expiry date using there properties to best fit your requirements.

 AddDays
 AddHours
 AddMilliseconds
 AddMinutes
 AddMonths
 AddSeconds
 AddTicks
 AddYears
 
After that I simply setup a Scheduled Task to run the Powershell script daily and I now have a Retention Policy on our temporary library.



No comments:

Post a Comment