当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:PowerShell 使用自定义操作注入 JavaScript

作者:精品下载站 日期:2024-12-14 14:37:02 浏览:13 分类:玩电脑

SharePoint Online:PowerShell 使用自定义操作注入 JavaScript


要求:使用 PowerShell 将 JavaScript 注入 SharePoint Online。

SharePoint Online 使用 PowerShell 注入 JavaScript:

让我们使用 PowerShell 将 JavaScript 插入 SharePoint Online,而不修改母版页!


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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 标签下。

[玩转系统] SharePoint Online:PowerShell 使用自定义操作注入 JavaScript

这将 JavaScript 添加到站点范围内!如果要将一段 JavaScript 添加到新式页面,请使用现代脚本编辑器 Web 部件:如何将 JavaScript 添加到 SharePoint 在线新式页面?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯