Everyone who has created a webpart for SharePoint, knows that the “developer experience” is not as good as one would expect. You create the webpart in Visual Studio, based on the Webpart Template, but you can’t design the User Interface by drag-and-dropping controls on the webpart. But luckily you can use the “User Control technique” which gives the advantage that you can use the VS.NET UI designer. Patrick Tisseghem has evangelized this technique a lot, check out his article, his posts and the video. Fons Sonnema has written an article about how you can create and use a generic webpart that can host an ASP.NET User Control. I’ve extended this webpart with some extra functionality; let me introduce you to the SmartPart for SharePoint!
The SmartPart is a generic webpart that can contain an ASP.NET user control. Nothing new you would say, but the SmartPart can give your user control access to the SharePoint object model. So from within the code of the User Control you can do stuff with SharePoint! Another feature is that the user control can expose some properties which the SmartPart picks up, so the user can specify values for these properties in the SharePoint site. To prove you how easy it is, I’ve create a small demo. In this demo I will create a DropDownNavigation webpart in less than 5 minutes (it could be less, but I made some typos). The webpart will show a dropdown list in which the subsites of a SharePoint site are displayed. When a site is selected from the list, the user will be redirected to that site. Here’s the video.
First I create a new ASP.NET Web Application solution in Visual Studio.NET. Then I add a new Web User Control to the project. If you want to use the SharePoint object model, you need to add references to the SmartPart assembly and the SharePoint assembly. Next I add some code. When I’m done, I build the project (actually I try to build it, correct a mistake and build again). To be able to use the user control in SharePoint, I copy the assembly to the \BIN folder of my SharePoint site, and the .ASCX file to a newly created \UserControls folder. Finally I show how you can use the SmartPart webpart in SharePoint to load the newly created User Control.
Together with Fons, I’ve created a GotDotNet Workspace for the SmartPart. Over there you can download an installation package that will install the SmartPart, the source code and an example user control. At this point there isn’t very much documentation, but I will work on that. :-) There are a lot of cool ideas which (I hope) will be added to the SmartPart (for example a connectable SmartPart). I’ve IM-ed with Patrick today and he has another cool idea which he discussed in one of his latest posts: using the User Interface Process block together with web parts. Anyway, some nice ideas are waiting to be implemented!