Create Page
Open your SharePoint site and create a list from the Tasks template.
Name it My Tasks as shown in the following:
You will get the tasks list as shown in the following:
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.
Create PowerShell Script
Now you can open the PowerShell ISE editor and add the following code.
- # Add Snapin
- if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
- {
- Add-PSSnapin "Microsoft.SharePoint.PowerShell"
- }
-
- # Update Page
- $web = Get-SPWeb http:
- $page = "/Lists/My%20Tasks/AllItems.aspx"
- $file = $web.GetFile($page)
-
- $file.CheckOut();
-
- $manager = $web.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
- $webpart = $manager.WebParts[0]
- $webpart.JSLink = "/siteassets/helloworld.js"
- $manager.SaveChanges($webpart);
-
- $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.
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.