SharePoint Online: Hide a Ribbon Button using Custom Action in PowerShell

Requirement: SharePoint Online Hide New Document Button in the ribbon.

PowerShell to Add Custom Action to Hide New Document Button of a Library

Here is the PowerShell to hide the new button in the SharePoint Online document library:

#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
}

This hides the new document button of the given document library in SharePoint Online. Similarly, you can hide any button in the SharePoint Online ribbon, like the upload button, edit page button, new item button, etc.

PowerShell to Remove Custom Action:

If you want to undo the changes by removing the custom action created, use this PowerShell script.

#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
}

You can also remove all custom actions of the given list or library by removing the where class at line #22.

You can also hide a button in the SharePoint Online toolbar using JSON: How to Hide a Button in SharePoint Online List or document library Toolbar?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: Hide a Ribbon Button using Custom Action in PowerShell

  • I am getting below error when i run the above script to hide

    Error Adding Custom Action! Exception calling “ExecuteQuery” with “0” argument(s): “Identity Client Runtime Library (IDCRL) did not get a response from the Login server.”

    Can you please help me in resolving

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *