Remote event reciever to allow renaming a files but prevent deleting them

john john 1,021 Reputation points
2023-07-23T19:20:05.81+00:00

We have a requirement inside our SharePoint online site, is that we need to allow renaming files inside document libraries but prevent deleting the files. now in SharePoint Permissions; rename & delete are treated the same. So if we define a custom permission level to allow users to add, edit but not to delete >> then this permission level will also not allow renaming files.

So any advice how we can allow users to rename files but without allowing them to delete the files using remote event receivers?

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-07-24T08:55:23.4933333+00:00

    Hi @john john,

    The following code snippet will prevent item deletion and also notify the user of what happened

    public override void ItemDeleting(SPItemEventProperties properties)
    {
        base.ItemDeleting(properties);
        properties.Status = SPEventReceiverStatus.CancelWithError;
        properties.ErrorMessage = "This item cannot be deleted; Contact your administrator";
    
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.