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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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