Update JS Link of Site Using PowerShell

Create Page

Open your SharePoint site and create a list from the Tasks template.

site content

Name it My Tasks as shown in the following:

adding task

You will get the tasks list as shown in the following:

my task

Copy the URL of the list. We will need it in the next step.

Note: Please note that we require %20 also to denote the space character.

Copy HelloWorld.js

Now create a HelloWorld.js file and copy it to the Site Assets library.

Site Assets library

Create PowerShell Script

Now you can open the PowerShell ISE editor and add the following code.

  1. # Add Snapin  
  2. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  3. {  
  4.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  5. }  
  6.  
  7. # Update Page  
  8. $web = Get-SPWeb http://sharepoint  
  9. $page = "/Lists/My%20Tasks/AllItems.aspx"  
  10. $file = $web.GetFile($page)  
  11.   
  12. $file.CheckOut();  
  13.   
  14. $manager = $web.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  15. $webpart = $manager.WebParts[0]  
  16. $webpart.JSLink = "/siteassets/helloworld.js"  
  17. $manager.SaveChanges($webpart);  
  18.   
  19. $file.CheckIn("JS Link updated");  
Change the Server URL appropriately and run the code. If the code is executed successfully, you will get your page updated.

Verification

You can verify the page update by opening the page in edit mode. Go to the web part properties and you can see that the JavaScript link was updated there.

JS link

This concludes the JavaScript Link updating using PowerShell.

Note: Please note that the preferred way of update is using CSOM code. In this way we can use the same code base for both SharePoint On-Premise and Online.

References

Summary

In this article we saw how to update the JavaScript Link of a site using PowerShell. You can get the code as an attachment.