Azure Runbooks, PowerShell and SharePoint

Translated from the Spanish original. Read in Spanish

This tutorial shows how to use an Azure Automation Runbook together with PowerShell to create a complex SharePoint page automatically. Through this process, you’ll be able to set up a new page with a tracking field, add a custom web part and handle header images, all from a script.

iStock AI Generator

Before you start, keep the prerequisites in mind:

  • This script uses Managed Identity for authentication. Make sure the Azure Automation account has a Managed Identity assigned and that it has the necessary permissions on the SharePoint site.

How to use Managed Identity with this script:

  1. Check that the Azure Automation account has a Managed Identity enabled.
  2. Grant the Managed Identity the necessary SharePoint permissions using the Azure Portal.
  3. The script will use the Managed Identity to authenticate to SharePoint without having to store credentials in the script.

Parameters

This block of code collects the parameters needed to run the script. You need to provide the SharePoint site URL ($siteurl), the page name ($page_name) and the site’s relative path ($relative_site).

Param
(
  [Parameter (Mandatory = $true)]  
  [string] $siteurl,
  [Parameter (Mandatory = $true)]
  [string] $page_name,
  [Parameter (Mandatory = $true)]
  [string] $relative_site
)

Connecting to SharePoint

Here we define the connection to SharePoint Online using the managed identity.

Connect-PnPOnline -Url $siteurl -ManagedIdentity

Variables for creating the page and image paths

We define the variables needed to create the page and the paths of the images that will be used as the header.

$pageRelativeImage="SiteAssets/ZerogapBanner.jpg"
$source_url_header="/sites/ZerogapSite/SiteAssets/zerogap/ZerogapBanner.jpg"

Check whether the specified page already exists

This code checks whether the page we’re trying to create already exists on the SharePoint site.

$FileExists = Get-PnPPage -identity $page_name -ErrorAction SilentlyContinue

Creating and configuring the page

If the page doesn’t exist, a series of commands runs to:

  1. Add a new taxonomy field to the SitePages library.
  2. Add a new page with the given name.
  3. Add a custom web part to the page.
  4. Prepare the header image and copy it to the desired location.
  5. Disable comments on the page.
  6. Update the page header with the new image.
  7. Publish the page and set the content owner.
If($FileExists)
{
    Write-output "Nothing to do for this site:"
    write-output  $siteurl
}
Else {
     Add-PnPTaxonomyField -List "SitePages" -DisplayName "Tracking" -InternalName "Archive" -TaxonomyItemId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'  -FieldOptions AddToAllContentTypes
    Add-PnPPage -Name $page_name
    $page_file=$page_name + ".aspx"

    Add-PnPPageWebPart -Page $page_file -Component "Zerogap Tracking"

    $PageHeaderImage = $relative_site + "/" + $pageRelativeImage
    $target_url=$relative_site + "/SiteAssets"
    $target_url=$target_url.replace('//','/')
    $DocumentLibrary = Get-PnPList -Identity "SiteAssets"

    if ($DocumentLibrary -eq $null) {
        $Web = Get-PnPWeb
        $Zerogap = $Web.Lists.EnsureSiteAssetsLibrary()
        $Zerogap = Invoke-PnPQuery
    }

    Copy-PnPFile -SourceUrl $source_url_header -TargetUrl $target_url -Force

    Set-PnPPage -Identity $page_file -CommentsEnabled:$false

    $page=Get-PnPClientSidePage -Identity $page_file
    $page.SetCustomPageHeader($PageHeaderImage)
    $page.Save()
    $Page.Publish()

    Set-PnPPage -Identity $page_file -Publish

    $values=@{"Editor"="admin@zerogap.uk"; "Author"="admin@zerogap.uk"}
    $id = Get-PnPPage -Identity $page_name | Select -ExpandProperty PageId
    $Zerogap = Set-PnPListItem -List "Site Pages" -Identity $id -Values $values -UpdateType UpdateOverwriteVersion

    Set-PnPPage -Identity $page_file -Publish
}

Disconnect the PnP Online session

Finally, after running the necessary actions, the PnP Online session is disconnected.

Disconnect-PnPOnline

By following these steps, you can automate the creation of a complex SharePoint page using an Azure Automation Runbook. Below is the complete code, without the comments and translated into Spanish.

Complete code

Param
(
  [Parameter (Mandatory = $true)]  
  [string] $siteurl,
  [Parameter (Mandatory = $true)]
  [string] $page_name,
  [Parameter (Mandatory = $true)]
  [string] $relative_site
)

$tenant='zerogap.onmicrosoft.com'
$AdminName = "admin@Zerogap.uk"
Connect-PnPOnline -Url $siteurl -ManagedIdentity

$pageRelativeImage="SiteAssets/ZerogapBanner.jpg"
$source_url_header="/sites/ZerogapSite/SiteAssets/Zerogap/ZerogapBanner.jpg"

$FileExists = Get-PnPPage -identity $page_name -ErrorAction SilentlyContinue

If($FileExists)
{
    Write-output "Nothing to do for this site:"
    write-output  $siteurl
}
Else {
    Add-PnPTaxonomyField -List "SitePages" -DisplayName "Tracking" -InternalName "Archive" -TaxonomyItemId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'  -FieldOptions AddToAllContentTypes
    Add-PnPPage -Name $page_name
    $page_file=$page_name + ".aspx"

    Add-PnPPageWebPart -Page $page_file -Component "Zerogap Tracking"

    $PageHeaderImage = $relative_site + "/" + $pageRelativeImage
    $target_url=$relative_site + "/SiteAssets"
    $target_url=$target_url.replace('//','/')
    $DocumentLibrary = Get-PnPList -Identity "SiteAssets"

    if ($DocumentLibrary -eq $null) {
        $Web = Get-PnPWeb
        $Zerogap = $Web.Lists.EnsureSiteAssetsLibrary()
        $Zerogap = Invoke-PnPQuery
    }

    Copy-PnPFile -SourceUrl $source_url_header -TargetUrl $target_url -Force

    Set-PnPPage -Identity $page_file -CommentsEnabled:$false

    $page=Get-PnPClientSidePage -Identity $page_file
    $page.SetCustomPageHeader($PageHeaderImage)
    $page.Save()
    $Page.Publish()

    Set-PnPPage -Identity $page_file -Publish

    $values=@{"Editor"="admin@zerogap.uk"; "Author"="admin@zerogap.uk"}
    $id = Get-PnPPage -Identity $page_name | Select -ExpandProperty PageId
    $Zerogap = Set-PnPListItem -List "Site Pages" -Identity $id -Values $values -UpdateType UpdateOverwriteVersion

    Set-PnPPage -Identity $page_file -Publish
}

Disconnect-PnPOnline

Maximiliano Díaz Doglia

AI Platform Engineer & Full-Stack Developer
Building Enterprise Integrations & Automations