SharePoint Online: รับสินค้าคงคลังเวิร์กโฟลว์โดยใช้ PowerShell
ความต้องการ: ค้นหาเวิร์กโฟลว์ทั้งหมดในไซต์คอลเลกชัน SharePoint Online และส่งออกรายการเวิร์กโฟลว์ไปยังรายงาน CSV
PowerShell เพื่อส่งออกสินค้าคงคลังเวิร์กโฟลว์ใน SharePoint Online:
การจัดการเวิร์กโฟลว์ใน SharePoint Online อาจเป็นเรื่องที่ท้าทาย โดยเฉพาะอย่างยิ่งเมื่อคุณมีเวิร์กโฟลว์จำนวนมากที่ทำงานบนไซต์และรายการต่างๆ โชคดีที่ PowerShell สามารถใช้เพื่อสร้างรายการเวิร์กโฟลว์ทั้งหมดใน SharePoint Online เพื่อให้คุณเห็นภาพรวมของเวิร์กโฟลว์ทั้งหมด ในบทความนี้ เราจะดูวิธีสร้างสินค้าคงคลังของเวิร์กโฟลว์ทั้งหมดในไซต์ SharePoint Online โดยใช้ PowerShell
สคริปต์นี้รับเวิร์กโฟลว์ทั้งหมดจากรายการและไลบรารีทั้งหมดจากคอลเลกชันไซต์ SharePoint Online ที่กำหนด และส่งออกข้อมูลเวิร์กโฟลว์เป็นไฟล์ CSV
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
#Function to Get workflows in a site
Function Get-SPOWorkflowInventory($SiteURL, $CSVPath)
{
Try{
$WorkflowInventory = @()
#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 and its Subsites
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.Webs)
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Initialize Workflow Manager Object
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
Write-host -f Yellow "Searching Workflows in Site: $SiteURL"
#Loop through each list and get all workflows sharepoint online powershell
ForEach($List in $Lists)
{
#Get SharePoint 2013 Workflows Associated with the List
$WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$Ctx.Load($WorkflowAssociations)
$Ctx.ExecuteQuery()
#Loop through each workflow associated with the List
Foreach ($Association in $WorkflowAssociations | Where {$_.Name -notlike "*Previous Version*"})
{
$WorkflowData = New-Object PSObject
$WorkflowData | Add-Member NoteProperty WorkflowName($Association.Name)
$WorkflowData | Add-Member NoteProperty SiteURL($Web.Url)
$WorkflowData | Add-Member NoteProperty ListName($List.Title)
Write-host -f Green "`t Found Workflow '$($Association.Name)' in list '$($List.Title)'"
$WorkflowInventory+=$WorkflowData
}
#Get SharePoint 2010 Workflows Associated
$WorkflowAssociations = $List.WorkflowAssociations
$Ctx.Load($WorkflowAssociations)
$Ctx.ExecuteQuery()
ForEach($Association in $WorkflowAssociations | Where {$_.Name -notlike "*Previous Version*"})
{
$WorkflowData = New-Object PSObject
$WorkflowData | Add-Member NoteProperty WorkflowName($Association.Name)
$WorkflowData | Add-Member NoteProperty SiteURL($Web.Url)
$WorkflowData | Add-Member NoteProperty ListName($List.Title)
Write-host -f Green "`t Found Workflow '$($Association.Name)' in list '$($List.Title)'"
$WorkflowInventory+=$WorkflowData
}
}
#Export Workflow data to CSV File
If($WorkflowInventory) { $WorkflowInventory | Export-CSV -LiteralPath $CSVPath -NoTypeInformation -Append}
#Process Subsites
Foreach($Subweb in $Web.Webs)
{
Get-SPOWorkflowInventory -SiteURL $Subweb.url
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$CSVPath = "C:\Temp\WorkflowInventory.csv"
#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to get workflow inventory
Get-SPOWorkflowInventory $SiteURL $CSVPath
รับอินสแตนซ์เวิร์กโฟลว์ที่ทำงานอยู่ทั้งหมดในรายการ SharePoint Online
หากต้องการรับรายการอินสแตนซ์ที่ทำงานอยู่ทั้งหมด เราต้องวนซ้ำแต่ละรายการในรายการและดึงข้อมูลสถานะเวิร์กโฟลว์:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/Retail"
$ListName ="Project Tasks"
#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 Item Objects
$Web = $Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$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()
#Define Query to get List Items in batch
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>2000</RowLimit></View>"
#Batch process list items - to mitigate list threshold issue on larger lists
Do {
#Get items from the list in batches
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each List Item
ForEach($ListItem in $ListItems)
{
#Get Workflow Instances of the List Item
$WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id)
$Ctx.Load($WorkflowInstanceCollection)
$Ctx.ExecuteQuery()
#Get all Workflow Instances in progress
ForEach ($WorkflowInstance in $WorkflowInstanceCollection | Where {$_.Status -eq "Started"})
{
[PSCustomObject] @{
ItemID = $ListItem.ID
Status = $WorkflowInstance.Status
WorkflowStarted = $WorkflowInstance.InstanceCreated
WorkflowAssociation = $WorkflowAssociations | where {$_.ID -eq $WorkflowInstance.WorkflowSubscriptionId} | Select -ExpandProperty Name
ItemUrl = "$($Web.Context.Web.Url)/$($workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"])"
StartedBy = $workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.InitiatorUserId"]
}
}
}
}While($Query.ListItemCollectionPosition -ne $null)
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
โดยสรุป การสร้างรายการเวิร์กโฟลว์ทั้งหมดใน SharePoint Online โดยใช้ PowerShell สามารถช่วยให้คุณจัดการเวิร์กโฟลว์ได้อย่างมีประสิทธิภาพมากขึ้น เมื่อทำตามสคริปต์ในบทความนี้ คุณจะสามารถสร้างรายการที่ครอบคลุมของเวิร์กโฟลว์ทั้งหมดในไซต์ SharePoint Online ของคุณได้อย่างรวดเร็ว ด้วยการใช้ประโยชน์จากพลังของระบบอัตโนมัติด้วย PowerShell คุณสามารถประหยัดเวลาและความพยายามในการจัดการเวิร์กโฟลว์ SharePoint Online ของคุณและมั่นใจได้ว่ากระบวนการทางธุรกิจของคุณยังคงมีประสิทธิภาพและประสิทธิผล
