How i can inject a javascript file inside my site collection using pnp-powershell [closed]
-
29-12-2020 - |
Question
I want to inject a JavaScript inside my sharepoint online site collection. now i find this command which sound provide the functionality i need Add-SPOJavaScriptLink
link
but i am not sure how i need to use this function inside my pnp-powershell script. now i wrote the following:-
#Credentials to connect to office 365 site collection url
$url ="https://XXXXX/sites/MainSite/"
$userName ="XXXXXXXX.com"
$password ="XXXXXXX"
Write-Host "Loading libraries"
Add-Type -Path (Resolve-Path "$env:Common\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:Common\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Write-Host "Authenticating to SharePoint Online Tenant site $url"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
#Credentials for the CSOM calls
$pass = convertto-securestring $password -asplaintext -force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $pass)
$Context.Credentials = $credentials
$Context.RequestTimeOut = 5000 * 60 * 10;
$site = $context.Site
$web = $site.RootWeb
$Context.Load($web)
$Context.Load($site)
$Context.ExecuteQuery()
#Credentials for the Connect-SPOnline command
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $(convertto-securestring $password -asplaintext -force)
Connect-SPOnline $web.Url -Credential $cred
Add-SPOJavaScriptLink -Name HomeLink -Url "XXXXXXXX/sites/MainSite/Style%20Library/myjavascript.js" –Scope Site
$Context.ExecuteQuery()
but the problem i am not sure how i can link the Add-SPOJavaScriptLink -Name HomeLink -Url "XXXXXXXX/sites/MainSite/Style%20Library/myjavascript.js" –Scope Site
with the current root web ?? as seems the Add-SPOJavaScriptLink
does not provide any parameter to define the root web url? so can anyone adivce on this please?
second question. now in my case i have site collection and 5 sub-sites. so will injecting the JavaScript inside the root web automatically inject it inside the sub-sites?? now i have enabled the publishing features inside the root web but not inside the sub-sites. so i can from the root web apply the master page to all the underlying sub-sites..so will this inject the JavaScript to all the sub-sites? or i have to manually inject the JavaScript to the sub-sites using pnp-powershell as i am trying to do inside the root web ? Thanks
La solution
Add-SPOJavaScriptLink
applies to the current context URL (in your case, https://XXXXX/sites/MainSite/) but you might have to connect using Connect-PnPOnline
such as Connect-PnPOnline –Url $url –Credentials (Get-Credential)
. What errors are you seeing?