使用 CSOM Powershell 关联批准工作流
Associate approval workflow using CSOM Powershell
我正在尝试将审批工作流附加到我的 SharePoint 库,但每次我都收到
'value cannot be null',
但我没有找到哪里出错了,
下面是我正在尝试的 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
}
}
我分享的以上代码绝对可以正常工作,仅此而已
您需要启用以下功能才能使用 SharePoint 2010 审批工作流。
我正在尝试将审批工作流附加到我的 SharePoint 库,但每次我都收到 'value cannot be null',
但我没有找到哪里出错了,
下面是我正在尝试的 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
}
}
我分享的以上代码绝对可以正常工作,仅此而已 您需要启用以下功能才能使用 SharePoint 2010 审批工作流。