PowerShell to Inject JavaScript or CSS using Custom Action in SharePoint

Requirement: Inject JavaScript or CSS in SharePoint without editing Master page

PowerShell to Inject JavaScript in SharePoint using Custom Action:
Assuming "ga.js" file is already uploaded to Site Assets library, here is the PowerShell for JavaScript injection in SharePoint master page.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="http://intranet.crescent.com"
$CustomActionTitle ="Google Analytics"

Try {
    #Get the Web
    $Web = Get-SPWeb $SiteURL
 
    #Check if the Custom Action Exists already
    $CustomAction = $web.UserCustomActions | Where { $_.Title -eq $CustomActionTitle }

    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $web.UserCustomActions.Add()

        #Set the Properties of the custom action
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "ScriptLink"
        $UserCustomAction.ScriptSrc = "~sitecollection/SiteAssets/ga.js"
        $UserCustomAction.Sequence = 1000
        $UserCustomAction.Update()
 
        Write-Host -f Green "Custom Action Added Successfully!"
    }
    Else
    {
        write-host -f Yellow "Custom Action Already Exists!"
    } 
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

Insert CSS using Custom Action - PowerShell
Similarly, you can insert CSS to master page using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="http://intranet.crescent.com"
$CustomActionTitle ="Custom Branding CSS"

Try {
    #Get the Site
    $Site = Get-SPSite $SiteURL
 
    #Check if the Custom Action Exists already
    $CustomAction = $Site.UserCustomActions | Where { $_.Title -eq $CustomActionTitle } | Select -First 1

    #Update the Custom action if found already
    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $Site.UserCustomActions.Add()
    }
    $CSSURL=$Site.URL+"/SiteAssets/Custom.css"
    #Set the Properties of the custom action
    $UserCustomAction.Title = $CustomActionTitle
    $UserCustomAction.Location = "ScriptLink"
    $UserCustomAction.ScriptBlock = "document.write('<link rel=""stylesheet"" After=""Corev15.css"" href=""$($CSSURL)"" type=""text/css""/>')"
    $UserCustomAction.Sequence = 1000
    $UserCustomAction.Update()
 
    Write-Host -f Green "Custom Action Updated Successfully!"
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

Here is my another post on JavaScript injection to SharePoint Online Master page Inject JavaScript into SharePoint Online
PowerShell to Inject JavaScript or CSS using Custom Action in SharePoint PowerShell to Inject JavaScript or CSS using Custom Action in SharePoint Reviewed by Salaudeen Rajack on July 10, 2017 Rating: 5

2 comments:

  1. When I do the JS injection, SharePoint 2010 site gives the error when loading
    An unexpected error occurred.
    When I remove the custom action site loads normally.
    Any suggestions for this issue?

    ReplyDelete
  2. Tried using your second snippet to inject stylesheet reference to a site through PS, and it worked BEAUTIFULLY. NOW, however, I need to modify it to do the exact same function on ALL of the sites in the farm. I am using a shared stylesheet that styles the 'maintenance mode' banner while we are migrating from SP Server 2016 to SP Online. I don't know if that means lines 7-31 should be inside of a for loop or something or what? I don't work inside of powershell much and am typically a front end person, but needed to convey this request to our SP Admin (who's PS skills are somehow comparable to mine...) Would appreciate ANY guidance or direction about how to essentially convert
    $SiteURL="{SPECIFIC-SITE-URL}"
    ** to correctly say this: **
    $SiteURL= { *DO THIS FUNCTION ONCE FOR EACH SITE RETURNED IN SITE COLLECTION *}
    Hope that makes sense. Seems like it should be an easy task!?!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.