Update JSLink using PowerShell

Customised a form using JSLink and want to provision using PowerShell? Here’s how:

# JSLink for Applications list
$Web = $Context.Web
$ApplicationsList = $Web.Lists.GetByTitle($ApplicationsListName)
$Context.Load($ApplicationsList)
Invoke-PnPQuery;
$Context.Load($ApplicationsList.Forms);
Invoke-PnPQuery;

# iterate through  DispForm.aspx, EditForm.aspx and NewForm.aspx
for ($i = 0; $i -lt 3; $i++) {
    $File = $Context.Web.GetFileByServerRelativeUrl($ApplicationsList.Forms[$i].ServerRelativeUrl)
    $Context.Load($File)
    $Context.ExecuteQuery()
    $WPManager = $File.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
    $WebParts = $WPManager.WebParts
    $context.Load($WebParts)
    $Context.ExecuteQuery()
    Set-PnPWebPartProperty -ServerRelativePageUrl $File.ServerRelativeUrl -Identity $WebParts[0].Id -Key "JSLink" -Value "~SiteCollection/Style Library/JS/ApplicationsForm.js"
    $Message = $File.ServerRelativeUrl + " UPDATED"
    Write-Output $Message
}

Leave a Reply

Your e-mail address will not be published. Required fields are marked *