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

[玩转系统] SharePoint Online:使用 PowerShell 添加自定义操作

作者:精品下载站 日期:2024-12-14 14:38:55 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 添加自定义操作


要求:使用 PowerShell 在 SharePoint Online 中创建自定义操作。

如何在 SharePoint Online 中添加自定义操作?

建议使用 SharePoint Online 中的用户自定义操作来自定义用户界面,例如向“网站设置”页面添加新链接、添加菜单项、功能区按钮组以及向 SharePoint 注入 JavaScript 或 CSS。以下是在 SharePoint Online 中添加自定义操作的示例:

SharePoint Online 使用 PowerShell 添加自定义操作

以下是在 SharePoint Online 中创建自定义操作的 PowerShell:


#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 = "SharePoint Admin Center"
 
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
    $Web = $Ctx.Web
    $UserCustomActions= $Web.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.Web.UserCustomActions.Add()

        #Set the Properties of the custom action
        $UserCustomAction.Name = $CustomActionTitle
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "Microsoft.SharePoint.SiteSettings"
        $UserCustomAction.Group = "SiteTasks" #Site Actions
        $UserCustomAction.Sequence = 1000
        $UserCustomAction.Url = "https://Crescent-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx"    
        $UserCustomAction.Update()

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

这将添加一个自定义操作以在 SharePoint Online 中的网站设置下创建链接:

[玩转系统] SharePoint Online:使用 PowerShell 添加自定义操作

PnP PowerShell 在 SharePoint Online 中添加自定义操作

使用 Add-PnPCustomAction cmdlet 在 SharePoint Online 中添加自定义操作。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"

#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Add Custom Action
Add-PnPCustomAction -Name 'SharePoint Admin Center' -Title 'SharePoint Admin Center' -Description "SPO Admin Center" -Location 'Microsoft.SharePoint.SiteSettings' `
        -RegistrationType List -Sequence 10000 -Group "SiteTasks" -Url "https://crescent-admin.sharepoint.com/"

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

取消回复欢迎 发表评论:

关灯