0

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

closed as too broad by Gautam Sheth, Dikesh Gandhi, Aakash Maurya, Gaurravs, Mohamed El-Qassas MVP Aug 8 '17 at 6:58

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

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?

  • now when i run the above script i got the following 2 errors. Connect-SPOnline : The term 'Connect-SPOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + Add-SPOJavaScriptLink : The term 'Add-SPOJavaScriptLink' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. – john Gu Aug 8 '17 at 0:42
  • 1
    Connect-SPOnline and Connect-PnPOnline are not the same thing. Sounds like you haven't actually installed PNP PowerShell module to your script. I assume you are using SharePoint online, thus you would need Install-Module SharePointPnPPowerShellOnline. You would have a much easier time if you read the documentation and try again. msdn.microsoft.com/en-us/pnp_powershell/pnp-powershell-overview – James Grizzle Aug 8 '17 at 1:39
  • now i download/install the pnp powershell setup file from the following link github.com/SharePoint/pnp-powershell/releases . then i restarted my windows 7 machine. i open the SP online management shell, try running the script but i got the same errors The term 'Connect-SPOnline' is not recognized & Add-SPOJavaScriptLink : The term 'Add-SPOJavaScriptLink' is not recognized .. while i can run the following command Connect-PnPOnline so i am not sure.. should i use Connect-PnPOnline instead of Connet-SPOnline?? – john Gu Aug 8 '17 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.