1

How to stop an Item fom being deleted, it seems that my code is not firing here is a snipped of the RER

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

            if (properties.EventType == SPRemoteEventType.ItemDeleting)
            {
                using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                {
                    if (clientContext != null)
                    {
                        try
                        {   

                            List list = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
                            ListItem listItem = list.GetItemById(properties.ItemEventProperties.ListItemId);

                            result.Status = SPRemoteEventServiceStatus.CancelWithNoError;

                            listItem["SuccessDeleting"] = "Yes 2.0";
                            listItem.UpdateOverwriteVersion();
                            clientContext.ExecuteQuery();
                        }
                        catch (Exception ex) { }
                    }
                }
            }

            return result;
        }

2 Answers 2

1

When your condition is matched then insert following code

properties.Cancel = true;

This will prevent deletion of the item.

2
  • What's the datatype of your properties? It did not work with my solution Sep 7, 2018 at 11:50
  • 1
    seems that your answer is for sharepoint-onpremise but I am using CSOM for sharepoint online Sep 7, 2018 at 12:01
1

One of two things is happening; either your RER is not being hit, or your code is not working properly. Here are some troubleshooting steps:

First, confirm your remote event receiver is properly registered. The easiest way to explain how to do this is to use the PnP PowerShell command Get-PnPEventReceiver against your list (use example 4 from the link). You should be able to see your RER in the list of event receivers.

Assuming it's registered, you next want to determine if your RER endpoint is being hit. Your IIS logs should be able to tell you if the requests are being sent. You should also have some logging in your RER code as well.

Once you've determined that your endpoint is actually being hit, your logging should be able to tell you what the problem is. When I looked at your code, the first thing that stood out to me was the empty catch block. Don't ever do this, put some logging in there.

The next thing I would suggest would be to simplify your code and see if you can get a simple implementation doing something, even if it's just a log statement. Updating the list item in the context of a synchronous ProcessEvent method seems weird to me, and I wouldn't be surprised if that's what's causing an exception to pass control to that empty catch block (notice I keep harping on that?).

Finally, I feel your pain, for remote event receivers are somewhat poorly understood. I've put together a blog post with some thoughts about them, give it a read here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.