ค้นหาเว็บไซต์

SharePoint Online: PowerShell เพื่อแทรก JavaScript โดยใช้การกระทำแบบกำหนดเอง


ความต้องการ: แทรก JavaScript ลงใน SharePoint Online โดยใช้ PowerShell

SharePoint Online แทรก JavaScript โดยใช้ PowerShell:

มาแทรก JavaScript ลงใน SharePoint Online โดยใช้ PowerShell โดยไม่ต้องแก้ไขเพจหลัก!


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com"
$CustomActionTitle = "Google Analytics"
 
Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get Existing Custom Actions
    $Site = $Ctx.Site
    $UserCustomActions= $Site.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    #Check if the CustomAction Exists already
    $CustomAction = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle } # | Select -First 1

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

        #Set the Properties of the custom action
        $UserCustomAction.Name = $CustomActionTitle
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "ScriptLink"
        $UserCustomAction.Sequence = 1
        $UserCustomAction.ScriptSrc ="~SiteCollection/SiteAssets/ga.js"
        $UserCustomAction.Description = "Google analytics Tracking Script"
        $UserCustomAction.Update()

        $Ctx.ExecuteQuery()
        Write-Host -f Green "Custom Action Added Successfully!"
    }
    Else
    {
         write-host -f Yellow "Custom Custom Already Exists!"
    }
}
Catch {
        write-host -f Red "Error Adding Custom Action!" $_.Exception.Message
}

สิ่งนี้จะแทรก JavaScript ไว้ใต้แท็ก u2063

นี่เป็นการเพิ่ม JavaScript ให้กับทั้งไซต์! ถ้าคุณต้องการเพิ่ม JavaScript ลงในหน้าสมัยใหม่ ให้ใช้ web part ตัวแก้ไขสคริปต์สมัยใหม่: วิธีเพิ่ม JavaScript ลงในหน้าสมัยใหม่ออนไลน์ของ SharePoint ได้อย่างไร

บทความที่เกี่ยวข้อง