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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.