使用CSOM Powershell关联批准工作流程

时间:2017-11-17 19:39:25

标签: powershell sharepoint sharepoint-online csom

我正在尝试将审批工作流程附加到我的SharePoint库,但每次都得到一个 '值不能为空',

但我找不到我错的地方,

下面的

是我正在尝试的PowerShell代码

是否有任何我缺少的参数值?

其他功能只是检查列表是否存在,如果不存在则创建它, 同时在创建列表时,我无法在方法createListByTemplate()

中找到列表模板f0r Task(2010)

由于 帕鲁

 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
        }

    }

1 个答案:

答案 0 :(得分:0)

我分享的上述代码完全可以正常使用它 您需要启用以下功能才能使用SharePoint 2010批准工作流程。

This feature needs to be enabled