none
Associate approval workflow using CSOM Powershell RRS feed

  • Question

  •  am trying to attach approval workflow to my SharePoint libraries but each time I get an  'value cannot be null',

    but I don't find where I am going wrong,

    below is my PowerShell code that I am trying

    Other functions are just to check of the list exists and if not then to create it,  also while creating the list I am unable to find the list template f0r Task (2010) in method createListByTemplate()

    Thanks Paru

    function TryGetList($spoCtx,$listName) { $web = $spoCtx.Web $lists = $web.Lists $spoCtx.Load($web) $spoCtx.Load($lists) $spoCtx.ExecuteQuery() $listExist = $web.Lists | where{$_.Title -eq $listName} if($listExist) { return $true } else { return $false } } function CreateListbyTemplate($spoCtx,$listName,$listTemplate,$listDescription) { try { $spoWeb=$spoCtx.Web $spoListCreationInformation=New-Object Microsoft.SharePoint.Client.ListCreationInformation $spoListCreationInformation.Title=$listName $spoListCreationInformation.Description = $listDescription $spoCtx.Load($web.ListTemplates) $spoCtx.ExecuteQuery() #$spoListCreationInformation.TemplateType=[int][Microsoft.SharePoint.Client.ListTemplatetype]::GenericList #$spoList=$spoWeb.Lists.Add($spoListCreationInformation) $spoListCreationInformation.ListTemplate = $web.ListTemplates | where {$_.InternalName -match $listTemplate } $spoWeb.Lists.Add($spoListCreationInformation) $spoCtx.ExecuteQuery() return $listName Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green Write-Host "List "$listName" created !!" -ForegroundColor Green $spoCtx.Dispose() } catch [Exception] { Write-Host -ForegroundColor Red "An Error Occured" $_.Exception.Message } } # $spoCtx has current context function attachApprovalWorkflow($spoCtx) { try { $SuccessLogs=$records.LogImportSuccess + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv" $FailureLogs=$records.LogImportFailure + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv" Add-Content $SuccessLogs “ID, Status”; Add-Content $FailureLogs “ID,Message”; [Microsoft.SharePoint.Client.Web]$web = $spoCtx.Web try { $liststoignore = @("Workflow History", "Workflow Tasks", "Master Page Gallery", "Composed Looks", "MicroFeed", "Site Assets", "Site Pages","Documents", "appdata", "appfiles", "Converted Forms", "Form Templates", "List Template Gallery", "Solution Gallery", "Style Library", "TaxonomyHiddenList", "User Information List", "Web Part Gallery", "Theme Gallery") [Microsoft.SharePoint.Client.ListCollection] $lstcoll = $spoCtx.Web.Lists; $spoCtx.Load($lstcoll); $spoCtx.ExecuteQuery() foreach($listsName in $lstcoll) { if ($listsName.BaseTemplate -eq 101 -and !$liststoignore.Contains($listsName.Title)) { [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listsName.Title) $spoCtx.load($list) $spoCtx.ExecuteQuery(); ################## $tasklist = TryGetList $spoCtx "Workflow Tasks" if ($tasklist -eq $false) { $tasklist = CreateListbyTemplate $spoCtx "Workflow Tasks" "Tasks (2010)" "Workflow Tasks" + " list" } else {[Microsoft.SharePoint.Client.List]$tasklist1 = $web.Lists.GetByTitle("Workflow Tasks") $spoCtx.load($tasklist1) $spoCtx.ExecuteQuery() } $historylist = TryGetList $spoCtx "Workflow History" if ($historylist -eq $false) { $historylist = CreateListbyTemplate $spoCtx "Workflow History" "Workflow History" "Workflow History" + " list" } else { [Microsoft.SharePoint.Client.List]$historylist1 = $web.Lists.GetByTitle("Workflow History") $spoCtx.load($historylist1) $spoCtx.ExecuteQuery() } ################### $workflowName = $listsName.Title + " Workflow" $wfApprovalWFTemplate = $web.WorkflowTemplates.GetByName("Approval - SharePoint 2010") $spoCtx.Load($wfApprovalWFTemplate) $spoCtx.ExecuteQuery() #Create a Workflow Association info $wfassociationInfo = New-Object Microsoft.SharePoint.Client.Workflow.WorkflowAssociationCreationInformation; $wfassociationInfo.ContentTypeAssociationTaskListName = $tasklist1 $wfassociationInfo.ContentTypeAssociationHistoryListName = $historylist1 $wfassociationInfo.Template = $wfApprovalWFTemplate $wfassociationInfo.Name = $workflowName $wfassociationInfo.TaskList =$web.Lists.GetByTitle("Workflow Tasks") $wfassociationInfo.HistoryList = $web.Lists.GetByTitle("Workflow History") #Associate to the Document Library where you want the approval WF to be added . Write-Host "Create a new Workflow Associationg with the Neccesary information." $wf = $list.WorkflowAssociations.Add($wfassociationInfo) $wf.AutoStartChange = $false $wf.AutoStartCreate = $false $wf.AllowManual = $true $wf.Update(); $spoCtx.ExecuteQuery() Write-Host -ForegroundColor Yellow "Workflow enabled on " $list.Title Add-Content $SuccessLogs $list.Title } } } catch [Exception] { Write-Host -ForegroundColor Red "An Error Occured" $list.Title $_.Exception.Message Add-Content $FailureLogs $listsName +","+$_.Exception.Message } } catch [Exception] { Write-Host -ForegroundColor Red "An Error Occured" $_.Exception.Message + $list.Title }

    }



    Paru

    Friday, November 17, 2017 7:43 PM

Answers

  • Hi,

    Have you enabled below feature for your site collection?

    You need enable below feature in order to use SharePoint 2010 approval workflow.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Paru Upreti Monday, November 20, 2017 1:21 PM
    Monday, November 20, 2017 1:06 PM

All replies

  • Hey Paru,

    Please try this below

    https://sharepoint.stackexchange.com/questions/122936/creating-sharepoint-2013-workflow-associations-with-powershell


    Please remember to click Mark as Answer on the answer if it helps you

    Friday, November 17, 2017 11:01 PM
  • This one as well

    https://sharepoint.stackexchange.com/questions/122936/creating-sharepoint-2013-workflow-associations-with-powershell


    Please remember to click Mark as Answer on the answer if it helps you

    Friday, November 17, 2017 11:01 PM
  • Thanks for your reply

    Both the urls are same , and I tried this but it didn't work :(

    error basically is this in this line

    $wf = $list.WorkflowAssociations.Add($wfassociationInfo)

    Any suggestions?

    Thanks

    Paru


    Paru


    • Edited by Paru Upreti Monday, November 20, 2017 10:32 AM
    Monday, November 20, 2017 10:31 AM
  • Hi,

    Have you enabled below feature for your site collection?

    You need enable below feature in order to use SharePoint 2010 approval workflow.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Paru Upreti Monday, November 20, 2017 1:21 PM
    Monday, November 20, 2017 1:06 PM
  • Thanks a ton Lee, you always help me !

    My code works now :)


    Paru

    Monday, November 20, 2017 1:21 PM