SharePoint Online:使用 PowerShell 中的自定义操作隐藏功能区按钮
要求: SharePoint Online 在功能区中隐藏新文档按钮。
PowerShell 添加自定义操作以隐藏库的新文档按钮
以下是用于隐藏 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/"
$ListName ="Team Documents"
$Location = "Ribbon.Documents.New.NewDocument"
$CustomActionTitle ="HideNewButton"
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 the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Get Existing Custom Actions
$UserCustomActions= $List.UserCustomActions
$Ctx.Load($UserCustomActions)
$Ctx.ExecuteQuery()
#Check if the CustomAction Exists already
$CustomAction = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle }
If($CustomAction -eq $Null)
{
#Add new custom action
$UserCustomAction = $List.UserCustomActions.Add()
#Set the Properties of the custom action
$UserCustomAction.Name = $CustomActionTitle
$UserCustomAction.Title = $CustomActionTitle
$UserCustomAction.Location = "CommandUI.Ribbon"
$UserCustomAction.CommandUIExtension = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location='$($Location)'/></CommandUIDefinitions></CommandUIExtension>"
$UserCustomAction.Sequence = 1000
#$UserCustomAction.RegistrationType = "List" # On Web or Site Scope, Use these two properties
#$UserCustomAction.RegistrationId = 101
$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
}
这将隐藏 SharePoint Online 中给定文档库的新文档按钮。同样,您可以隐藏 SharePoint Online 功能区中的任何按钮,例如上传按钮、编辑页面按钮、新项目按钮等。
PowerShell 删除自定义操作:
如果您想通过删除创建的自定义操作来撤消更改,请使用此 PowerShell 脚本。
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName ="Team Documents"
$CustomActionTitle="HideNewButton"
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
$List = $Ctx.Web.lists.GetByTitle($ListName)
$UserCustomActions= $List.UserCustomActions
$Ctx.Load($UserCustomActions)
$Ctx.ExecuteQuery()
#Check if the CustomAction Exists
$CustomActions = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle }
If($CustomActions -ne $Null)
{
ForEach($CustomAction in $CustomActions)
{
$CustomAction.Deleteobject()
Write-Host -f Green "Custom Action Removed Successfully:" $CustomAction.Name
}
$Ctx.ExecuteQuery()
}
Else
{
write-host -f Yellow "Custom Actions Does Not Found!"
}
}
Catch {
write-host -f Red "Error Removing Custom Action!" $_.Exception.Message
}
您还可以通过删除第 22 行的 where 类来删除给定列表或库的所有自定义操作。
您还可以使用 JSON 隐藏 SharePoint Online 工具栏中的按钮:如何隐藏 SharePoint Online 列表或文档库工具栏中的按钮?