Archive for September, 2009

Speed up deployment of SharePoint solution on a development machine

September 28, 2009

This post will help you to speed up the deployment on a developers machine if you use a batch file that is called from a postbuildevent on the project.
The deployment of a SharePoint solution usually consists of following parts:
1. Copying assemblies to GAC
2. Copying files to 12 folder
3. Reinstalling/reactivating features
4. iisreset/recycling SharePoint apppool
Resetting apppool instead of iisreset will save you also some time

The idea of speeding up is that many times we can skip the step 3. Actually we need the step 3 only when we modify the xml of features or introduce a new feature.
So we will split our postbuild.bat file to two files 1. postbuild.bat, 2. installfeatures.bat. In the installfeatures.bat there will be deactivating/uninstalling/installing/activating of all features. In the postbuild.bat will stay the rest.
Then we will create our new Solution Configuration. So click on the DropDown on the toolbar in Visual Studio, where Debug is usually displayed. Then click Configuration Manager. In the Configuration Manager create a new Solution Configuration named Quick Debug, that will be based on Debug mode.
Now open the project settings and modify the postbuild event as following:

call "$(ProjectDir)postbuild.bat"
IF "$(ConfigurationName)"=="Quick Debug" (call "$(ProjectDir)installfeatures.bat")

Now if we want to Quick deploy the solution we just select the configuration Quick Debug. If we play around with feature we will use Debug or Release configuration.

Display a custom page on item deleting

September 28, 2009

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" />

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