SPO custom action to hide delete button

Brass Contributor

Hello,

 

I need, in a list, when select an item, to hide in the ribbon the delete button (at least); I've seen that maybe this is possible with a custom action and find powershell code to implement it, but it doesn't work; the code is this:

 

#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://mysite.sharepoint.com/sites/testsite"
$ListName ="MyList"
$Location = "Ribbon.ListItem.Manage.Delete"
$CustomActionTitle ="HideDeleteButton"
 
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.Site.lists.GetByTitle($ListName)
    $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 = 100
        $UserCustomAction.Update()
        $Ctx.Load($UserCustomAction)
        $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
}

 

When I execute this, I obtain the error:

 

Eccezione durante la chiamata di "ExecuteQuery" con "0" argomento/i: "Setting this property is not supported.  A value of List has already been set and cannot be changed."
In riga:1 car:1
+ $Ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

 

The ExecuteQuery that give the error is that on the row 47 in the code; I don't know, also, if the $Location value is exact (I've taken it on Default Server Ribbon Customization Locations | Microsoft Docs).

 

Any help is appreciated.

 

--

Regards

2 Replies

@Marco Mangiante If you want to remove the delete button, you can also create a custom permission level (like Contribute without delete) and grant limited permissions to users.

 

Is there any other scenario/requirement where user need higher permissions on the same list/library?


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Helllo @ganeshsanap 

 

thanks for your reply; I tried it but I have a drawback: in my list I created a custom column that host a link to a flow that cancel that particular row; now, if I set the permission like you suggested, when I click on the link the flow doesn't start..but it not only doesn't start, I haven't the form that appears on the right side of the list that let me run it.

 

--

Regards