Event Receiver ItemDeleting with CancelWithRedirectUrl “Event Receiver has canceled the request” – SOLVED

After much troubleshooting on an odd error “Event Receiver has canceled the request” when trying to redirect a user to a custom error page, I discovered that SharePoint is picky about the RedirectUrl you pass in.  When using properties.RelativeUrl on a root web application it returns “/” when using it on a non-root site collection it returns something like “sites/mysite” (with no ending slash).  So – if you are developing on a non-root site collection and deploy to a root site collection, your redirect URL may contain a double slash.  This double slash was the problem for me.

Checking for an ending slash fixed the issue.

SPList list = properties.List;

SPListItem item = list.GetItemById(properties.ListItemId);

string itemName = item[“Title”].ToString();

string itemId = item[“ID”].ToString();

string relativeUrl = properties.RelativeWebUrl;

if (relativeUrl.EndsWith(“/”) == false) { relativeUrl = relativeUrl + “/”; }

properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

properties.RedirectUrl = relativeUrl + “Lists/Foo/CustomDelete.aspx?ItemName=” + itemName + “&itemId=” + itemId;

One thought on “Event Receiver ItemDeleting with CancelWithRedirectUrl “Event Receiver has canceled the request” – SOLVED

Leave a Reply

Your email address will not be published. Required fields are marked *