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:
- Do a HttpContext.Current.Response.Redirect in OnItemDeleting of a custom item event receiver. This does not work because the HttpContext.Current is null.
- Create my own instance HttpContext a do a redirect through this custom generated context. This does not work either.
- 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.
September 29, 2009 at 6:20 am |
Jojo to se bude hodit.
JC