A SharePoint list is a structured collection of records whose fields store request data, selectable values, links, and processing status. SharePoint can generate a default form from the list, while PowerShell customizes the list and its fields. section 11.2.2
Designing a list for automation involves:
- Creating a generic list with New-PnPList and adding fields with Add-PnPField. section 11.2.2
- Using text, URL, choice, and lookup fields to represent request properties and related records. section 11.2.2
- Predefining status values such as Submitted, Creating, Active, Retired, and Problem. section 11.2.2
- Updating status before processing so a watcher does not submit the same request twice. section 11.3.2
Site request workflow
A Site Templates list stores available templates and is prepopulated from Get-PnPWebTemplates. A Site Requests list then captures the site name, URL, status, and selected template through an automatically generated form. A watcher finds Submitted items, changes each to Creating, and passes only its item ID to an action script, which retrieves the remaining data. section 11.2.2 section 11.3.2
Listing 11.1 Monitoring for new site requests
$ClientId = '<Your Client GUID>' #1
$Thumbprint = '<Your Certificate Thumbprint>'
$RequestSite = "https://<subdomain>.sharepoint.com/sites/SiteManagement"
$Tenant = '<subdomain>.onmicrosoft.com'
$ActionScript = ".\Listing 2.ps1" #2
$RequestList = 'Site Requests' #3
$PnPOnline = @{ #4
ClientId = $ClientId
Url = $RequestSite
Tenant = $Tenant
Thumbprint = $Thumbprint
}
Connect-PnPOnline @PnPOnline
$Query = @' #5
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Status'/>
<Value Type='Text'>Submitted</Value>
</Eq>
</Where>
</Query>
</View>
'@
$submittedSites = Get-PnPListItem -List $RequestList -Query $Query
foreach ($newSite in $submittedSites) {
$Arguments = "-file ""$ActionScript""", #6
"-ListItemId ""$($newSite.Id)"""
$jobParams = @{
FilePath = 'pwsh'
ArgumentList = $Arguments
NoNewWindow = $true
ErrorAction = 'Stop'
}
$PnPListItem = @{ #7
List = $RequestList
Identity = $newSite
Values = @{ Status = 'Creating' }
}
Set-PnPListItem @PnPListItem
try {
if (-not (Test-Path -Path $ActionScript)) { #8
throw ("The file '$($ActionScript)' is not recognized as " +
"the name of a script file. Check the spelling of the " +
"name, or if a path was included, verify that the path " +
"is correct and try again.")
}
Start-Process @jobParams -PassThru #9
}
catch {
$PnPListItem['Values'] = #10
@{ Status = 'Problem' }
Set-PnPListItem @PnPListItem
Write-Error $_
}
}
Inspect the watcher implementation that connects to SharePoint, queries for Submitted items, updates their status, and starts the action script. section 11.3.2
