Adil Baig's Tech Blog

Wednesday, January 16, 2008

Getting application Context in Sharepoint Event Handler

Hello Folks,

I hope you found some use in the Custom Query Based Lookup Field Control and the improved, fortified, Vitaminized Parent Child Control(.. I'd actually spend more time on the later, filling up the holes, then what i did in my own). Ne ways, and they lived happily ever after. So lets move on. ......

In one of our project, we had many columns in custom list that were not supposed to be exposed to business user, for e.g., if a trainee fills his end of program report (basically a list item). his tutor for the program should populate automatically. So the obvious approach that any one would go for would be:

1. Hide the column in the New/Edit item page (using sharepoint designer)
2. And populate their values (from might be other master/ reference custom list) using event handler

But things started getting messy when the event would raise error. It just dont go on the default Sharepoint error page and i get a classic ASP.NET 2.0 YSOD (yeah.. i started calling it Yellow screen of death recently!!) with a more ridiculous message which i guess most sharepoint developer must be aware( and annoyed) of : Data Source Failed to execute insert command

so lets say if i did a :



I am suppose to get a "Dude..now thats not allowed!!" on an error page that exploits the default sharepoint master.But no... it has to screw up every were doesnt it?? Believe me..."Data Source Failed to execute insert command" on a classic ASP.NET 2.0 YSOD with a stack trace is certainly not what your or mine client would like to see.

then i tried the same on a simple virgin custom list( with no sharepoint designer modification.. or master modification). every thing is perfectly how its suppose to be. Now i dint really had time to find out whether i screwed up or did the sharepoint product team??. All i wanted was that YSOD to go away (shooo..shoo)
so the simple and logical and fast solution to this was obviously :
1. Make your own custom error page with the sharepoint default master
2. dump the myError.aspx and myError.aspx.cs in the sharepoint layouts folder( thats where the actual error page also reside)
3. and on error ...redirect to my custom page and display my message there.

simple ha!!.... 1. and 2. again went smooth but then, as i said.., it has to screw up somewhere doesnt it?? to redirect i need HttpContext (n i will kick his ass who says no). but then a miracle happened.

public void ItemAdding (SPItemEventProperties properties)
{
HttpContext current = HttpContext.Current;
}

i get HttpContext.Current as a Big O' null!!! what the heck.. and i thought all the time that this guy runs 'inproc' with my w3p process. 10,000 questions all at a time started hammering ma head.. how can this be possible????. it has to run inproc!!... if it doesnt.. then is it a remote assembly call???.. how can it be???... if not.. then y is my HttpContext null???
Googled around.. blogged people.. begged people.. no one answers.. every one's with the same complain... infact i even had a thought : "enough.!!!!!............. first thing i m doin is taking an IJB to a much chilled out .NET 2.0 custom solutions floor.. yeah..(back to SqlConnection and OdbcConnection days)."

I really dont know what was going on in my mind when i tried this out as a desperate last attempt:


public class MyEventHandler : SPItemEventReceiver
{
HttpContext current;

public MyEventHandler ()
{
current = HttpContext.Current;
}
}


Hallelujah!!!!!!!! .. i get the Context. Try it in ItemAdding ...nope... try again in the default constructor !!!!!!.
Till now couldnt figure out the funda behind this, but finally i was getting my error message on my custom error page ,which, this time was even better then the default one..hahahah!!!... you dont want to show yourself up. Well, i dont need you Mr. ErrorPage.aspx... i have better one. with my full UI grooming capabilities

So the Moral of the story is ..(if you have forgotton as to from where did it all started):

plz avoid page Customization using designer if not "really" needed or might be do it in a more controlled manner( and what that would be i have no clue).
ne ways.. all you people (and i, by now, know many) who faced this ..the solution is simple!! ... initialize your context in the default constructor. will put it up on my esnips page very soon

And

Coming up next...(after 3 certifications that are queued up my ass)
A Client using Humming Bird Document management system (by oracle) with over 50,000 documents in store (and still growing ) want it all in sharepoint within 20 days.
Can this be done...(keep checkin out my space)



Friday, January 04, 2008

Sharepoint 2007 Custom Field - Query Based Lookup - II

Thanks for all the comments and suggestion, (and my personal R&D) that i could fix lots of bugs and loop holes in the control (Both Custom query based lookup and parent child control).
The updated copy can be found on my esnipspage as usual.

See you guys soon with my new utility that i am working on called "doc dumper".

Sunday, November 18, 2007

Sharepoint 2007 Custom Field - Query Based Lookup

First off all, i would like to thank DataCog Guys to show newcomers (specifically in MOSS) like me that thing like this can be done in first place.
So this was my problem. i was into a sharepoint project that had requirements so funny that made me think twice as to why are these guys opting for sharepoint 07 in first place.Y cant they happily do it through a .net 2.0 custom web app.
Well, thats a different story all together.
In short, one of those funny requirements was to get highly filtered data in a dropdown based list column. The "out-of-the-box" Lookup column is a plain lookup thats simply gives all the values of a particular column of a particular List in your List(It does not even take care to display distinct values ... lol)
my requirements were way to much for this lookup guy to handle.Yes, we are talkin about full fledged query's here (like select [column] from [list] where [column1] = [value] and [column2] = [value] and [column3] <= [value]..... so on and on) We First thought that web parts are only bet in this case. When i found out this article of these Datacogs guys.(once again.. all hail DataCogs) However, even these guys are solving their own specific problem of cascading dropdowns. I feel, my requirement was far too generic then this and i m sure 100s and 1000s did faced it and are facing it still. My plans were, why not give people a full fledged query building interface wherin, they can put as many filtering criteria's as they wish..(like the one i pointed above) So this is what i came up with following the same idea of the that DataCogs article The Project is now moved to CodePlex
Sorry, i continued on his project and after every thing was done, i was too lazy to remove them. so just dont look at them, Unless, offcourse you are interested in cascading stuff.
By the way, the only beauty of his control is a simple 4 line method called
FindControlRecursive. Cool stuff!!!
Any ways, a Big Thanks to DHTML goodies guys for making my fancy balloon toolTip dream come true in this control
Hoping of a response for bugs or feature enhancement at
baig.adil@gmail.com

Tags: ,