I need to use the js for all views in all libraries in a site. Is there a way to deploy this in one hit rather than editing each view individually?
2 Answers
You can use powershell:
$site = Get-SPSite http://mysite
$web = $site.OpenWeb()
$list = $web.Lists["List Name"]
$wp = $list.RootFolder.Name + "/Forms/AllItems.aspx"
$wpManager = $website.GetLimitedWebPartManager($wp, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = $wpManager.WebParts[0]
$webpart.JSLink = "~SiteCollection/_catalogs/masterpage/myScript.js"
$wpManager.SaveChanges($webpart)
You can loop throught your lists to add the js script.
-
Thanks how do I specify a specific site?Mike Blair– Mike Blair2015-04-23 14:43:07 +00:00Commented Apr 23, 2015 at 14:43
-
-
I am getting these errors You cannot call a method on a null-valued expression. Cannot index into a null array. The property 'JSLink' cannot be found on this object. Verify that the property exists and can be set. Any ideas?Mike Blair– Mike Blair2015-04-23 15:09:49 +00:00Commented Apr 23, 2015 at 15:09
-
The
$webpartis null execute the script line for line to check if you retrive the right objectNk SP– Nk SP2015-04-23 15:11:44 +00:00Commented Apr 23, 2015 at 15:11
Change $website to $web , the final script will look like below :
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb http://sharepointsite
$list = $web.Lists["TestJSLink"]
$wp = $list.RootFolder.Name + "/Forms/AllItems.aspx"
$wpManager = $web.GetLimitedWebPartManager($wp, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = $wpManager.WebParts[0]
$webpart.JSLink = "~layouts/XYZ/JSLink.js"
$wpManager.SaveChanges($webpart)
Write-Output "Done"