Wednesday, August 24, 2011

PowerShell Script: Create Content Type, Assign Retention and Audit Policies, Publish Via Content Hub



First I need to make sure to identify the source of most of this script: Phil Childs at http://get-spscripts.com/.  He has a great site with lots o' PowerShell scripts for SharePoint.

I needed a script that could read from an input file (this case I used a CSV) and create a Content Type, assign retention and audit policies to it, and publish it afterwards (this was on a content type syndication hub).   I took several different scripts and cobbled them together for the following:

$SiteColl = read-host "What is the URL of the target site collection?";
$ParentContentType = read-host "What is the Parent Content Type?"
$CTGroup = read-host "What Content Type Group Should These Be Created Under?";

$CTList = Import-Csv ContentTypes.txt

ForEach ($item in $CTList){
$Record = $($item.Record)
$Description = $($item.Description)
$Property = $($item.Property)
$Period = $($item.Period)
$PeriodType = $($item.PeriodType)

$site = get-spsite $SiteColl
$web = $site.openweb()
$ctypeName = $Record
$ctypeParent = $web.availablecontenttypes[$ParentContentType]

<# Create Content Type #>

$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
$ctype.Description = $Description
$ctype.group = $CTGroup
$ctype = $web.contenttypes.add($ctype)
write-host $Record : "Content Type Created" -foregroundcolor green;

<# Set Retention and Audit Policies #>

if (($Period -ne "IND") -or ($Period -notcontains "MAX")) {
$contentType = $web.ContentTypes[$Record];[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();
 write-host $Record : "Retention Policy of" $Period $PeriodType "assigned." -foregroundcolor yellow;
 write-host " ";
 }
else{
 write-host "No Retention Policy Required" -foregroundcolor DarkCyan;
 write-host " ";
}

<# Publish Content Type #>
$Publisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($SiteColl)
Write-Host "Publishing the content type.";
$Publisher.Publish($ctype);

}

The import file had the following formatting:

Record,Description,Property,Period,PeriodType
Billing,Billing for Completed Invoices,Document_x0020_Date,6,years

  • Record = Name of Content Type
  • Description = Populates Description Field of Content Type
  • Property = Internal Name of Field to Base Retention Policy On
  • Period = How Long of a Retention Period
  • PeriodType = Years, Months, Days
Maybe I will work on adding exception handling on the next go around.

Jeff

    1 comment:

    1. "CreatePolicy($contentType, $null)"
      Is it ok?

      ReplyDelete