Developing SharePoint 2007 Web Parts

Excerpt from Real World SharePoint 2007: Indispensable Experiences From 16 MOSS and WSS MVPs

By Jan Tielens

Web Parts are the building blocks of pages in SharePoint sites. Users of SharePoint sites can make use of those building blocks to determine what should be displayed on a specific page in a particular SharePoint site.

When you install SharePoint, you can make use of some out-of-the-box Web Parts straight away. Depending on whether you have Windows SharePoint Services (WSS) or Microsoft Office SharePoint Server (MOSS) as your SharePoint installation, you'll have more or less. Additionally, every SharePoint list and document library will have a Web Part counterpart that can display the contents of the corresponding list or document library.

Of course, the out-of-the-box Web Parts are not the only ones that you can use! Developers can build their own Web Parts as well and deploy them to the SharePoint server. End users won't notice the difference between the custom Web Parts and the out-of-the-box Web Parts, so Web Parts are a great way to extend SharePoint.

This article takes you through the basic steps to create your own Web Parts in various ways.

TIP
The techniques and technologies described in this article are applicable both to Windows SharePoint Services (WSS) and Microsoft Office SharePoint Server (MOSS), unless mentioned otherwise. So, in this article, mention of SharePoint should be interpreted as Windows SharePoint Services 3.0 or Microsoft Office SharePoint Server 2007.

TIP
Although you can create Web Parts making use of the SharePoint 2003 classes, and use those Web Parts in SharePoint 2007 sites, you should make use of ASP.NET 2.0 Web Parts for your new projects.

Writing the Code

A Web Part in code is just a normal .NET class, nothing more, nothing less. In Visual Studio, you can make use of the Class Library project template to write code for the Web Part class. This code will be compiled into a .NET assembly, in this case a DLL that is exactly what you need. When Visual Studio is started, create a new project and select the Class Library template (Figure 1). The name of the new project is important, so think carefully when you choose a project name. Your project name should be unique on the server on which you would like to deploy your Web Parts. Additionally, the project name will be used later when the Web Parts are deployed. Also, be aware that names in .NET are case-sensitive!

Figure 1
Figure 1: Selecting the Class Library template

TIP
A common practice to ensure unique names in .NET environments is using namespaces. If the project name is, for example, MVP.Book.WebParts, by default, all the code will be sitting in the namespace with exactly the same name.

Every Class Library project in Visual Studio can contain any number of Web Part classes, so think of the project as the container of your Web Parts.

When the new project is created, there will be one class already available: Class1.cs. In general, Class1 is not a good name for a Web Part, so rename the class to HelloWorld. The special thing about a Web Part class is the fact that the class inherits from a specific base WebPart class. This base WebPart class is available in the System.Web assembly (Figure 2). By default, the Class Library project doesn't contain a reference to this assembly, so you should add it yourself. Right-click the project node in the Solutions Explorer window and choose Add Reference. Next, select the System.Web assembly from the list.

Figure 2
Figure 2: Locating the System.Web assembly

Now the HelloWorld class can inherit from the WebPart that is available in the System.Web.UI.WebControls.WebParts namespace class.

Tags: