PowerShell เพื่อฉีด JavaScript หรือ CSS โดยใช้ Custom Action ใน SharePoint
ข้อกำหนด: แทรก JavaScript หรือ CSS ใน SharePoint โดยไม่ต้องแก้ไขมาสเตอร์เพจ
PowerShell เพื่อแทรก JavaScript ใน SharePoint โดยใช้การกระทำแบบกำหนดเอง:
สมมติว่าไฟล์ “ga.js” ได้รับการอัปโหลดไปยังไลบรารี Site Assets แล้ว นี่คือ PowerShell สำหรับการแทรก JavaScript ในหน้าหลักของ 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
}
แทรก CSS โดยใช้การกระทำแบบกำหนดเอง - PowerShell
ในทำนองเดียวกัน คุณสามารถแทรก CSS ลงในเพจหลักได้โดยใช้ 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
}
นี่คือโพสต์อื่นเกี่ยวกับการแทรก JavaScript ไปยังหน้าต้นแบบของ SharePoint Online แทรก JavaScript ลงใน SharePoint Online