liveBook · Manning

SharePoint list

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A SharePoint list is a structured collection of records stored in SharePoint, with fields that hold item data and status information.

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

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Descends through list creation, field configuration, lookup XML, status transitions, and watcher integration, but not form customization beyond defaults.
Examples
Uses Site Templates and Site Requests lists with template choices, site URLs, statuses, lookup relationships, and monitored submissions.
What it assumes
Assumes familiarity with PowerShell scripts and introduces the required SharePoint list and field construction through PnP cmdlets.
What you can do after
Build it from scratch by creating configurable lists, generating forms, populating records, and coordinating request processing scripts.
How it connects
Connects SharePoint forms, PowerShell automation, lookup fields, status fields, watcher scripts, and action scripts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.