kwizcom banner advertisement

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

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

SharePoint Online: PowerShell to Add Custom Action To Hide New Document Button of a Library
Here is the PowerShell to hide new button in 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://crescenttech.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 new document button of the given document library in SharePoint Online. Similarly, you can hide any button in SharePoint Online ribbon like 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://crescenttech.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
SharePoint Online: Hide a Ribbon Button using Custom Action in PowerShell SharePoint Online: Hide a Ribbon Button using Custom Action in PowerShell Reviewed by Salaudeen Rajack on July 09, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.