当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 对列表中的所有项目启动工作流

作者:精品下载站 日期:2024-12-14 15:00:15 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 对列表中的所有项目启动工作流


要求:使用 PowerShell 在 SharePoint Online 中运行工作流。

[玩转系统] SharePoint Online:使用 PowerShell 对列表中的所有项目启动工作流

SharePoint Online:用于启动工作流的 PowerShell

SharePoint Online 中的工作流可帮助组织自动化业务流程并提高工作效率。如果您需要对列表中的所有项目启动工作流程 - 这可能是一个耗时且乏味的过程。 PowerShell 可用于启动 SharePoint Online 列表中所有项目的工作流。本文将介绍如何使用 PowerShell 为列表中的所有项目启动 SharePoint Online 工作流。

此 PowerShell 脚本针对 SharePoint Online 列表中的所有项目启动给定工作流:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com/Sites/PMO"
$ListName ="Project Tasks"
$WorkflowName ="Send Approval Email"

#Get Credentials to connect
$Cred= Get-Credential

Try{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
    #Get the Web, List and List Items Object
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $Ctx.Load($List)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Initialize Workflow Manager and other related objects
    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
    $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() 
    $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
    $Ctx.Load($WorkflowAssociations)
    $Ctx.ExecuteQuery()

    #Get the Target workflow to Run
    $WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
  
    #Start Workflow on each List Item
    ForEach($ListItem in $ListItems)
    {
        #sharepoint online powershell start workflow        
        $Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
        $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociation, $ListItem.Id, $Dict)
    }
    $Ctx.ExecuteQuery()
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}

此 PowerShell 在 SharePoint Online 列表的每个项目中运行工作流。

PnP PowerShell 在 SharePoint Online 中启动工作流

这次,我们使用 PowerShell 对特定项目运行特定的 SharePoint Online 工作流。以下是如何使用 PnP PowerShell 触发 SharePoint Online 工作流:


#Parameters
$SiteUrl = "https://crescent.sharepoint.com/sites/ICPapers"
$ListName = "IC Docs"
$WorkflowName = "Set Document Permissions"
$ItemID = 23

#Connect to SharePoint Online 
Connect-PnPOnline $SiteUrl -Interactive
 
#Get the Workflow to Run
$WFSubscription = Get-PnPWorkflowSubscription -List $ListName -Name $WorkflowName

#Start Workflow 
Start-PnPWorkflowInstance -Subscription $WFSubscription -ListItem $ItemID

总之,使用 PowerShell 启动 SharePoint Online 列表中所有项目的工作流是一个简单而高效的过程,可以帮助您自动化业务流程并提高工作效率。通过使用本指南中提供的脚本,您可以快速启动列表中所有项目的工作流程,而无需手动干预。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯