Debugging User Controls in your SharePoint Web Part

Although a lot of people don’t think it is possible, today i found that it is possible. After building my user control, i discovered that hosting it in the SmartPart did not do the trick. Under the hood, the user control calls an assembly to add a user to Active Directory. Although we could run the code if we used a a web part; the code would throw a generic exception once it was executed by the user control.

And that made sense; I am happy that I was not able to use a .acsx and it .cs file for this kind of work. Would not be very secure.. But I needed to find a way to do this. Microsoft uses the Control templates folder in the 12 hive to store its user controls. Hmm.. that folder might be configured to have some more rights and permissions to run code.. Also, I saw that third party components (for example K2) used this folder to store their user controls in. So it was quickly decided to follow their example.

The problem now was that the SmartPart will not let you choose another folder then the "[virtual directory]/Usercontrols" one (unless you want to alter their code of course). No worries; a custom web part which loads the user control dynamically was quickly build. And… yes! It worked!

By accident I also noticed that i was suddenly able to debug my user control! Apparently, the ControlTemplates folder has some additional configuration so Visual Studio picks it up automatically. Just attach the web part to the w3p processes, and it will automatically load the user control if you create an instance of it in your web part code.

Leave a comment