Skip to main content

Cloud

Visual Web Parts in SharePoint 2007

Level: Beginner
One of the new features of SharePoint 2010 is Visual Web Parts. However, this approach is really not that new. Developers, including myself, have been using this technique since we first started to develop SharePoint 2007 web parts. The idea is simple: create an ASP.NET user control and wrap it in SharePoint web part or ASP.NET web part (ASP.NET web part works just fine in SharePoint). The benefit of using ASP.NET user controls is obvious; you get visual UI and you don’t have to write HTML code with concatenated strings in your web part code-behind. In this blog, I will show you a simple example of what you need to do to make it work. You will need to deploy this web part to your server via SharePoint solution package (not covered in this blog). If you need help with SharePoint deployment packages, search the web for STSDEV or WSPBuilder.
Create a new ASP.NET web application project and add a new web user control.
clip_image002
Figure 1: ASP.NET user control.
clip_image004
Figure 2: Visual Studio designer provides DESIGN area for ASP.NET user controls.
As shown in figure 2 above, user controls provide visual design interface. You can simply drag and drop controls from toolbox to the design surface, configure them and add event handlers to them. If you are new to ASP.NET and not familiar with ASP.NET user controls, see this MSDN article: http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx.
Finally, you need to create a web part class to wrap your user control in it. ASP.NET web parts work fine in SharePoint, so I will just create a simple ASP.NET web part for this example.
image
Figure 3: ASP.NET Web Part
This web part is simple. It overrides “CreateChildControls” method and add the user control to its “Controls” collection. Notice that I used “/_ControlTemplates/” folder to deploy the user control. SharePoint keeps out of the box user controls in “ControlTemplates” folder, so I used the same folder in this example. The physical location of this folder on your computer should be C:Program FilesMicrosoft SharedCommon FilesWeb Server ExtensionsTemplateControlTemplates.
That’s all there is to it. Obviously, SharePoint 2010 and Visual Studio 2010 provides a lot of new enhanced developer features such as developer dashboard and deployment functionality, but if you are using SharePoint 2007, you should try this technique to make your SharePoint web part development easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.