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>  



 param(  
 $url=$null,  
 $ContentTypeName=$null)  
 $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)  
   {  
   [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint')   
   [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Office.Policy')  
   [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.AddWorkflowAssociation($association)  
       $ct.UpdateWorkflowAssociationsOnChildren($true, $true, $true, $false)  
       $association.Enabled=$true  
       $ct.Update()  
       $web.Update()  
     }  
     else  
     {  
     Write-Error "Workflow Template not found"  
           Add-Content $logFileName "Workflow Template not found."  
     }  
   }  
 function StartWorkflow($site, $wfAssociationName, $listName, $contentType, $WfStartColumn)  
 {  
   $contentType = $web.ContentTypes[$contentType]  
   if($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()  
     $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='workflow' id='"+ $wfAssociationId +"' />"+  
      "</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!!!"  
  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!!

Comments

  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 ??
    Thanks!!

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Search Result Webpart,Filter result If QueryString is present in URL

SharePoint 2013 Workflow gets canceled automatically

Permission denied for "Add and Customize Pages" in Sharepoint Online