SPO custom action to hide delete button

%3CLINGO-SUB%20id%3D%22lingo-sub-3221306%22%20slang%3D%22en-US%22%3ESPO%20custom%20action%20to%20hide%20delete%20button%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3221306%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%2C%20in%20a%20list%2C%20when%20select%20an%20item%2C%20to%20hide%20in%20the%20ribbon%20the%20delete%20button%20(at%20least)%3B%20I've%20seen%20that%20maybe%20this%20is%20possible%20with%20a%20custom%20action%20and%20find%20powershell%20code%20to%20implement%20it%2C%20but%20it%20doesn't%20work%3B%20the%20code%20is%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Load%20SharePoint%20CSOM%20Assemblies%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%0A%20%20%20%0A%23Set%20parameter%20values%0A%24SiteURL%3D%22https%3A%2F%2Fmysite.sharepoint.com%2Fsites%2Ftestsite%22%0A%24ListName%20%3D%22MyList%22%0A%24Location%20%3D%20%22Ribbon.ListItem.Manage.Delete%22%0A%24CustomActionTitle%20%3D%22HideDeleteButton%22%0A%20%0ATry%7B%0A%20%20%20%20%23Get%20Credentials%20to%20connect%0A%20%20%20%20%24Cred%3D%20Get-Credential%0A%20%20%20%20%24Credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24Cred.Username%2C%20%24Cred.Password)%0A%20%20%20%0A%20%20%20%20%23Setup%20the%20context%0A%20%20%20%20%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%0A%20%20%20%20%24Ctx.Credentials%20%3D%20%24Credentials%0A%20%0A%20%20%20%20%23Get%20the%20List%0A%20%20%20%20%23%24List%20%3D%20%24Ctx.Site.lists.GetByTitle(%24ListName)%0A%20%20%20%20%24List%20%3D%20%24Ctx.Web.lists.GetByTitle(%24ListName)%0A%20%0A%20%20%20%20%23Get%20Existing%20Custom%20Actions%0A%20%20%20%20%24UserCustomActions%3D%20%24List.UserCustomActions%0A%20%20%20%20%24Ctx.Load(%24UserCustomActions)%0A%20%20%20%20%24Ctx.ExecuteQuery()%0A%20%0A%20%20%20%20%23Check%20if%20the%20CustomAction%20Exists%20already%0A%20%20%20%20%24CustomAction%20%3D%20%24UserCustomActions%20%7C%20Where%20%7B%20%24_.Title%20-eq%20%24CustomActionTitle%20%7D%0A%20%0A%20%20%20%20If(%24CustomAction%20-eq%20%24Null)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%23Add%20new%20custom%20action%0A%20%20%20%20%20%20%20%20%24UserCustomAction%20%3D%20%24List.UserCustomActions.Add()%0A%20%0A%20%20%20%20%20%20%20%20%23Set%20the%20Properties%20of%20the%20custom%20action%0A%20%20%20%20%20%20%20%20%24UserCustomAction.Name%20%3D%20%24CustomActionTitle%0A%20%20%20%20%20%20%20%20%24UserCustomAction.Title%20%3D%20%24CustomActionTitle%0A%20%20%20%20%20%20%20%20%24UserCustomAction.Location%20%3D%20%22CommandUI.Ribbon%22%0A%20%20%20%20%20%20%20%20%24UserCustomAction.CommandUIExtension%20%3D%20%22%3CCOMMANDUIEXTENSION%3E%3CCOMMANDUIDEFINITIONS%3E%3CCOMMANDUIDEFINITION%20location%3D%22%24(%24Location)%22%3E%3C%2FCOMMANDUIDEFINITION%3E%3C%2FCOMMANDUIDEFINITIONS%3E%3C%2FCOMMANDUIEXTENSION%3E%22%0A%20%20%20%20%20%20%20%20%24UserCustomAction.Sequence%20%3D%201000%0A%20%20%20%20%20%20%20%20%24UserCustomAction.RegistrationType%20%3D%20%22List%22%20%23%20On%20Web%20or%20Site%20Scope%2C%20Use%20these%20two%20properties%0A%20%20%20%20%20%20%20%20%24UserCustomAction.RegistrationId%20%3D%20100%0A%20%20%20%20%20%20%20%20%24UserCustomAction.Update()%0A%20%20%20%20%20%20%20%20%24Ctx.Load(%24UserCustomAction)%0A%20%20%20%20%20%20%20%20%24Ctx.ExecuteQuery()%0A%20%0A%20%20%20%20%20%20%20%20Write-Host%20-f%20Green%20%22Custom%20Action%20Added%20Successfully!%22%0A%20%20%20%20%7D%0A%20%20%20%20Else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20write-host%20-f%20Yellow%20%22Custom%20Custom%20Already%20Exists!%22%0A%20%20%20%20%7D%0A%7D%0ACatch%20%7B%0A%20%20%20%20%20%20%20%20write-host%20-f%20Red%20%22Error%20Adding%20Custom%20Action!%22%20%24_.Exception.Message%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20execute%20this%2C%20I%20obtain%20the%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3EEccezione%20durante%20la%20chiamata%20di%20%22ExecuteQuery%22%20con%20%220%22%20argomento%2Fi%3A%20%22Setting%20this%20property%20is%20not%20supported.%20%20A%20value%20of%20List%20has%20already%20been%20set%20and%20cannot%20be%20changed.%22%0AIn%20riga%3A1%20car%3A1%0A%2B%20%24Ctx.ExecuteQuery()%0A%2B%20~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20ServerException%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20ExecuteQuery%20that%20give%20the%20error%20is%20that%20on%20the%20row%2047%20in%20the%20code%3B%20I%20don't%20know%2C%20also%2C%20if%20the%20%24Location%20value%20is%20exact%20(I've%20taken%20it%20on%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Foffice%2Fdeveloper%2Fsharepoint-2010%2Fee537543(v%3Doffice.14)%3Fredirectedfrom%3DMSDN%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDefault%20Server%20Ribbon%20Customization%20Locations%20%7C%20Microsoft%20Docs%3C%2FA%3E).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20is%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3221306%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3222053%22%20slang%3D%22en-US%22%3ERe%3A%20SPO%20custom%20action%20to%20hide%20delete%20button%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3222053%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F36354%22%20target%3D%22_blank%22%3E%40Marco%20Mangiante%3C%2FA%3E%26nbsp%3BIf%20you%20want%20to%20remove%20the%20delete%20button%2C%20you%20can%20also%20create%20a%20custom%20permission%20level%20(like%20Contribute%20without%20delete)%20and%20grant%20limited%20permissions%20to%20users.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20any%20other%20scenario%2Frequirement%20where%20user%20need%20higher%20permissions%20on%20the%20same%20list%2Flibrary%3F%3C%2FP%3E%3CHR%20%2F%3E%3CP%3EPlease%20click%26nbsp%3B%3CSTRONG%3EMark%20as%20Best%20Response%3C%2FSTRONG%3E%26nbsp%3B%26amp%3B%20%3CSTRONG%3ELike%3C%2FSTRONG%3E%20if%20my%20post%20helped%20you%20to%20solve%20your%20issue.%20This%20will%20help%20others%20to%20find%20the%20correct%20solution%20easily.%20It%20also%20closes%20the%20item.%20If%20the%20post%20was%20useful%20in%20other%20ways%2C%20please%20consider%20giving%20it%20%3CSTRONG%3ELike%3C%2FSTRONG%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3222081%22%20slang%3D%22en-US%22%3ERe%3A%20SPO%20custom%20action%20to%20hide%20delete%20button%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3222081%22%20slang%3D%22en-US%22%3E%3CP%3EHelllo%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F373269%22%20target%3D%22_blank%22%3E%40ganeshsanap%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20for%20your%20reply%3B%20I%20tried%20it%20but%20I%20have%20a%20drawback%3A%20in%20my%20list%20I%20created%20a%20custom%20column%20that%20host%20a%20link%20to%20a%20flow%20that%20cancel%20that%20particular%20row%3B%20now%2C%20if%20I%20set%20the%20permission%20like%20you%20suggested%2C%20when%20I%20click%20on%20the%20link%20the%20flow%20doesn't%20start..but%20it%20not%20only%20doesn't%20start%2C%20I%20haven't%20the%20form%20that%20appears%20on%20the%20right%20side%20of%20the%20list%20that%20let%20me%20run%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3C%2FLINGO-BODY%3E
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