Monday, September 3, 2012

PowerShell To create Retention Policies for Content Types

Hi Friends,

PowerShell is really a very powerful tool. Its not just a command prompt, we can also do coding into this.
Here I am using Powershell to create Policies on Content Types. I am creating Retention Policies and Auding Policy on Content Types. I am using One file "D:\Test\ContentTypes.txt" which contains data(content types) description according which we are applying Policies on those.

Data in the "D:\Test\ContentTypes.txt" will be in following format:

Name,Property,Period,PeriodType
One CTypeName,Created,15,Years
Two CTypeName,Created,25,Years
Three CTypeName,Created,Permanent,Years

function  abc()
{
$url = "http://server:1717/sites/TestTopLevelSite/"

$site = get-spsite $url
$web = $site.openweb()

Enable-SPFeature –identity "LocationBasedPolicy" -URL $url -ErrorAction SilentlyContinue

$CTList = Import-Csv D:\Test\ContentTypes.txt

echo "Processing Content Types"

ForEach ($item in $CTList){

$Name = $($item.Name)
$Property = $($item.Property)
$Period = $($item.Period)
$PeriodType = $($item.PeriodType)

$contentType = $web.ContentTypes[$Name]

if (($Period -ne "IND") -or ($Period -notcontains "MAX")) {

if($contentType){

[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($contentType, $null);

$newPolicy = [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($contentType);

$newPolicy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration",
 "<Schedules nextStageId='3'>"+
 "<Schedule type='Default'>"+
 "<stages>"+
 "<data stageId='1' stageDeleted='true'></data>"+
 "<data stageId='2'>"+
 "<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>"+
 "<number>"+$Period+"</number>"+"<property>"+$Property+"</property>"+
 "<period>"+$PeriodType+"</period>"+
 "</formula>"+
 "<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin' />"+
 "</data>"+
 "</stages>"+
 "</Schedule>"+
 "</Schedules>");

$newPolicy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.PolicyAudit",
 "<Audit>"+
 "<Update />"+
 "<View />"+
 "<CheckInOut />"+
 "<MoveCopy />"+
 "<DeleteRestore />"+
 "</Audit>");

$newPolicy.Update();

}
}
}

$web.Dispose()
$site.Dispose()

echo "Finished!!!"

}

Happy Reading!!!

2 comments:

  1. Hi i tried this and it appers to work but in the uls log i am getting

    The Policy Expiration Formula is corrupted. Period 'Years' is not a valid value

    ReplyDelete
  2. Hi.. Can you please explain the XML part? What does each tag mean?

    ReplyDelete