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

john john 946 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?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,003 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,757 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,862 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,476 Reputation points Microsoft Vendor
    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.