In the event of a SharePoint Event Handler, don’t…

In the event of an Event Handler, don’t try to do anything “fancy”.  Why?  Because SharePoint doesn’t allow you to.  Sure, there are tons of events and fancy things that you can do on the server side, but there is a serious limitation.  The “fancy” stuff I want to do is simply to work with the browser and the user – in the ASP.NET world.  However, Event Handlers are implemented as Class Library assemblies and do not have web context.  So that means I cannot access the DOM, use JavaScript or System.Web, or redirect the browser to another page after something happens (such as when a document is uploaded to a library, when an item is added to a list, when a site is deleted, etc).  You cannot even present any kind of information to the user – such as a success prompt or custom HTML with supporting information – except in the case of an error!!  Even then, you only have control over the error string presented to the user – no customization of the page it is displayed on.  Sure you can get access to the entire Microsoft.SharePoint namespace – but nothing that I’ve found there allows you to punch through the ceiling to create anything resembling a common web application.

I have looked far and wide with no success to find anything that can tell me definitively that you can or cannot get web context in an Event Handler.  Apparently it is standard with ASP.NET web applications that if you need to reference a custom Class Library assembly that needs web context, all you need to do within that assembly is add a reference to System.Web and write code that accesses HttpContext.Current.  The web context will supposedly be inherited by the assembly from the web application that is referencing it and running inside of IIS.  I tried this – SharePoint returns Nothing.

So unless somebody can show me the way, I have to wonder why SharePoint is blocking this – or why the SharePoint team decided not to go the extra mile.  Being able to better interact with the user would open up Event Handler solutions to create a better experience with SharePoint for advanced business applications.

BTW – if you plan on working with Event Handlers in Survey Lists, they work a little differently compared to other lists.  If your Survey has a page break, clicking Next will trigger the ItemAdded event!  Further page breaks call ItemUpdated, and ItemAdded isn’t even called when the final question is answered – ItemUpdated is called again.  To handle this, look at evaluating any of these properties to determine whether the user has fully completed the survey:

Eric Legault

Microsoft Outlook MVP (15 yrs). Author: Microsoft Office 2013 Pro Step By Step. Consultant/speaker/Outlook add-in builder/SharePoint pioneer. Guitarist: Maiden Canada (Iron Maiden tribute band)

You may also like...

%d bloggers like this: