PowerShell para inyectar JavaScript o CSS usando una acción personalizada en SharePoint
Requisito: Inyecte JavaScript o CSS en SharePoint sin editar la página maestra.
PowerShell para inyectar JavaScript en SharePoint mediante una acción personalizada:
Suponiendo que el archivo "ga.js" ya esté cargado en la biblioteca de recursos del sitio, aquí está el PowerShell para la inyección de JavaScript en la página maestra de SharePoint.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Set Site variable
$SiteURL="https://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
}
Insertar CSS usando acción personalizada - PowerShell
De manera similar, puede insertar CSS en la página maestra usando PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Set Site variable
$SiteURL="https://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
}
Aquí hay otra publicación sobre la inyección de JavaScript en la página maestra de SharePoint Online Inyectar JavaScript en SharePoint Online