SharePoint Caveats – 1: Redirecting in Item Deleting event receiver.

I have been working in SharePoint for the past 2 years. I have encountered numerous bugs in SharePoint which makes me bewildered about Microsoft’s carelessness. So, I have decided to write a series of blog posts about the bugs in SharePoint (2010).

This is my first post in the series. In this I’m going to describe the bug that I found in the Item Deleting event receiver in SharePoint compared to the Item Adding event receiver.

In SharePoint event receivers we have options to cancel an event from occurring. We can also redirect to an application page or a web part page after canceling the event from occurring.

Here is the code for Canceling an Item Adding event from occurring and redirecting to an Application page .

           properties.Cancel = true;
           properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
           properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";
           this.EventFiringEnabled = false;

Let us see what is the bug in SharePoint event receiver with respect to the above coding.

1. Create an Event receiver project with Item adding and Item deleting event receivers targeting Document Library template..

2. Now, add an Application Page to your project.

3. In the Item Adding event receiver add the above code as shown below.

public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           properties.Cancel = true;
           properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
           properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";
           this.EventFiringEnabled = false;
       }

4. Deploy the solution in to SharePoint. Go to any of the document libraries in your SharePoint site, add a document and you will see that you are being redirected to an application page as shown below.

Untitled

5. Now, comment the code in the Item Adding event receiver and copy & paste the same code in to the Item Deleting event receiver as shown below.

public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);

           properties.Cancel = true;
           properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
           properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";
           this.EventFiringEnabled = false;
       }

Deploy the solution.

6. Now, go to any of the document libraries and try to delete a document by clicking the delete option in the context menu of the document or from the ribbon menu.

7. You will see that you are not being redirected to the application page (as shown below) as in Item Adding event receiver.Untitled

As you see above you are taken to the default SharePoint error page instead of the application page that we created.

Alright, fine do not panic we still have our code that we implemented above in the Item deleting event receiver functioning somewhere inside SharePoint.

I will show where.

In the context menu of the document click View Properties, you will see a pop up with the properties of the document. In the ribbon menu of the pop up click Delete Item as shown below.

Untitled

And now you will be taken to the custom Application page that we had created as below.
Untitled

Fine, we have successfully finished discussing about a SharePoint bug. Thanks

Update: This blog post was discussed  here in the SharePoint forum. As you can see there, an explanation was given by a Microsoft guy saying “While deleting an item, the HTTPContext may be null. In this case, the URL redirection might fail, and we treat CancelWithRedirctUrl as CancelWithError to show the error message to the user.“, that being said, will try to come with a solution.

Advertisements

11 thoughts on “SharePoint Caveats – 1: Redirecting in Item Deleting event receiver.

  1. Pingback: SPListItem event receiver maddness » the .corner

  2. On a side note, to make the Error popup (in #7 above) a little more “informative” we can set the properties.ErrorMessage as shown below.

    properties.ErrorMessage = String.Format(“Custom error message here”);

  3. Pingback: SPListItem event receiver madness – Journeyman's Cradle

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s