Display a custom page on item deleting

Scenario:
A user wants to delete an item, which for some reason should not be deleted. e.g. master detail relation with another item. The request is to display a custom page that will contain additional information about the deletion (the standard info message is not enough). I tried following scenarios that do not work:

  1. Do a HttpContext.Current.Response.Redirect in OnItemDeleting of a custom item event receiver. This does not work because the HttpContext.Current is null.
  2. Create my own instance HttpContext a do a redirect through this custom generated context. This does not work either.
  3. Assign a link to the custom message that is displayed when the item cannot be deleted. This link should point to the custompage with the additional info. This does not work either because the text is encoded and the href is displayed as a text.

Now what works:
I created a Custom SPItemEventReceiver and ion ItemDeleting I check if the item can be deleted and if so then set Cancel = true. Then I created a custom HttpModule that checks every request coming to the server. This module finds out if this is a this page that is shown when the item cannot be deleted. If true then the response is redirected to my custom page. You can see the source code below:

using System;
using System.Web;
using Microsoft.SharePoint;
using System.Globalization;
using Communardo.LinkChecker.CustomPages;
using Communardo.LinkChecker.DataAccess;
using System.Linq;

namespace CustomNamespace
{
    public class ShowCustomDeletePageModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(ExecuteHttpModule);
        }

        private void ExecuteHttpModule(object sender, EventArgs e)
        {
            if (DeletingItem())
            {
                string url = HttpContext.Current.Request.QueryString["owsfileref"];
                if ((!string.IsNullOrEmpty(url)) && (!DeletingAllowed(url)))
                {
                    HttpContext.Current.Response.Redirect(SPContext.Current.Web.Url + "/_layouts/CannotDeletePage.aspx" + HttpContext.Current.Request.Url.Query);
                }
            }
        }

        private static bool DeletingAllowed(string url)
        {
     //do the business logic if the item can be deleted           
        }

        private static bool DeletingItem()
        {
            return ((HttpContext.Current.Request.Url.ToString().Contains("owssvr")) && (HttpContext.Current.Request.Url.ToString().Contains("Cmd=Delete")));
        }
    }
}
 

The HttpModule has to be registered in the web.config. You can do it manually or via SPWebConfigModification. Here is the web.config entry:

<add name="CustomDeletePageModule" type="CustomNameSpace.ShowCustomDeletePageModule,  four part strongly signed assembly name" />

Note:
There can be a performance hit because the check is done every request, but I hope it will not be critical.

Advertisements

One Response to “Display a custom page on item deleting”

  1. Jméno Says:

    Jojo to se bude hodit.
    JC

Leave a Reply

Please log in using one of these methods to post your comment:

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


%d bloggers like this: