Sean Carter

Ramblings about SharePoint, C# and ASP.NET

Add HttpModule to SharePoint

leave a comment »

Followed this excellent article to create a custom handler for adding new users which works great. Only fiddly bit is getting the web config entries right…here’s what mine looked like after a bit of tweaking (note that the format is: type=”[new module type that implements IHttpModule], [assembly, not namespace], Version, Culture, PublicKeyToken)


Web Config entry for http module in SharePoint web config

Here is my variation on Sohail’s code which includes a licence confirmation checkbox so that admins have to check a box to confirm that the user(s) they are adding have a CAL / CALs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace SharePoint2010.Portal.HTTPModules
{
    public class LicenceConfirmationModule : IHttpModule
    {
        Page page;
        private CheckBox _licenceConfirmation;

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication httpApp = sender as HttpApplication;

            if (httpApp != null)
            {
                page = httpApp.Context.CurrentHandler as Page;
                if (page != null)
                {
                    if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("_layouts/aclinv.aspx"))
                    {
                        page.Init += new EventHandler(page_Init);
                    }
                }
            }
        }

        void page_Init(object sender, EventArgs e)
        {
            if (page != null)
            {

                Control ctrl = page.Form.FindControl("PlaceHolderMain").Controls[1].FindControl("PlaceHolderControls").Controls[1].FindControl("PlaceHolderControl").FindControl("userPicker"); //get reference to the "Add User"s control
                _licenceConfirmation = new CheckBox();
                _licenceConfirmation.Text = "Please confirm that the users you're adding have a SharePoint license";

                CustomValidator cust1 = new CustomValidator();
                cust1.ServerValidate += new ServerValidateEventHandler(cust1_ServerValidate); // Handle the event to perform validation
                cust1.ControlToValidate = _licenceConfirmation.ID;
                cust1.Text = "Unable to add users who do not have a license."; //set the error message to be displayed if the validation fails
                HtmlGenericControl licenseCheckboxDiv = new HtmlGenericControl("DIV");
                HtmlGenericControl licenseValidatorDiv = new HtmlGenericControl("DIV");

                licenseCheckboxDiv.Controls.Add(_licenceConfirmation);
                licenseValidatorDiv.Controls.Add(cust1);

                ctrl.Parent.Controls.AddAt(ctrl.Parent.Controls.IndexOf(ctrl) + 1, licenseValidatorDiv);
                ctrl.Parent.Controls.AddAt(ctrl.Parent.Controls.IndexOf(ctrl) + 1, licenseCheckboxDiv); //add the custom validator just after the Add Users control

            }
        }

        void cust1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (_licenceConfirmation.Checked == false)
            {
                args.IsValid = false; //fail the validation
                return;
            }
        }



        public void Dispose()
        {

        }

       
    }
}




License confirmation

Advertisements

Written by Sean Carter

August 6, 2012 at 11:12 am

Posted in C#, SharePoint 2010

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

%d bloggers like this: