Powershell To Attach workflow on Content Type and create Retention Policy on it - SharePoint 2013

Hi Friends,

I got a requirement to create retention policy on Content type and action is to start workflow. as well attach workflow to the content type.

I choose to go with powershell.

save the following code into file called SetupRetentionPolicy.ps1

call like:

 c:\>.\SetupRetentionPolicy.ps1 -url <weburl> -ContentTypeName <Contenttype>  

 $site = get-spsite $url  
 $web = $site.openweb()  
 Enable-SPFeature –identity "LocationBasedPolicy" -URL $url -ErrorAction SilentlyContinue  
 $Property = "Modified"  
 $Period = 1;  
 $PeriodType = "years"  
 function AddWorkflowToContentType($site, $ctName, $WfName, $WfAssociationName, $TaxTaskList)  
   [Guid]$wfTemplateId = New-Object Guid  
     $web = $site.RootWeb  
     $ct = $web.ContentTypes[$ctName]  
     $culture = New-Object System.Globalization.CultureInfo("en-US")  
     $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)  
     if($template -ne $null)  
     $tasklist = $TaxTaskList  
     $historylist = "IP Workflow History"  
   # Workflow Association with Content Type  
       $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateSiteContentTypeAssociation($template, $WfAssociationName, $web.Lists[$tasklist], $web.Lists[$historylist])   
       $association.AllowManual = $false  
       $association.AutoStartCreate = $true  
       $ct.UpdateWorkflowAssociationsOnChildren($true, $true, $true, $false)  
     Write-Error "Workflow Template not found"  
           Add-Content $logFileName "Workflow Template not found."  
 function StartWorkflow($site, $wfAssociationName, $listName, $contentType, $WfStartColumn)  
   $contentType = $web.ContentTypes[$contentType]  
     [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::CreatePolicy($contentType, $null);  
     $newPolicy = [Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($contentType);  
      $wfAssociationId = $contentType.WorkflowAssociations.GetAssociationByName($wfAssociationName, 1033).ParentAssociationId.ToString()  
      "<Schedules nextStageId='3'>"+  
      "<Schedule type='Default'>"+  
      "<data stageId='1' stageDeleted='true'></data>"+  
      "<data stageId='2'>"+  
      "<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>"+  
      "<action type='workflow' id='"+ $wfAssociationId +"' />"+  
      "<Update />"+  
      "<View />"+  
      "<CheckInOut />"+  
      "<MoveCopy />"+  
      "<DeleteRestore />"+  
 echo "Finished!!!"  
  AddWorkflowToContentType -site $site -ctName $ContentTypeName -wfName "DocumentApprovalWorkflow" -WfAssociationName "DocumentApproval" -TaxTaskList "Improvement Tasks";  
  StartWorkflow -site $site -contentType $ContentTypeName -wfAssociationName "DocumentApproval" -listName "Documents" -WfStartColumn "Modified";  

Workflow should be availble on web when you run this script.
workflow is of sharepoint 2010 reusable type.

Happy Coding!!


  1. Hi Anuja,
    Why you choose for Powershell script. I have similar requirement but on SharePoint Online. And I am not getting option to attach content type usnig SPD.
    Any thoughts ??

  2. Thanks for this information. Would you happen to have a script that could identify all lists that have a retention policy that uses a workflow. I'm been trying to figure it out, using your script but I am unsuccessful.

  3. How to achieve same requirement in SharePoint online. I have been working on it am not succeeded yet. Can you please help with code.


Post a Comment

Popular posts from this blog

SharePoint 2013 Workflow gets canceled automatically

All about Microsoft Search and SharePoint Online Search

Configure notes as attachments for entity forms - Missing "Attach a File" option