Welcome!

AJAX & REA Authors: Charlene Qu, Yakov Fain, Andreas Grabner, Lori MacVittie, Kevin Hoffman

Related Topics: .NET

.NET: Article

How To Design, Build, and Deploy Custom Web Parts

Extending portals by building custom modules

The next level is Windows SharePoint Services 3.0 or WSS. This free component of Windows Server 2003 leverages the ASP.NET Web Parts framework and offers the entire infrastructure needed to create a basic Web portal including pre-build functionality for user management, document collaboration, and workflow.

Finally, you can use Microsoft Office SharePoint Server 2007, or MOSS. This product builds on WSS and offers much tighter integration with Microsoft Office, including better document management and search features, wikis, and blogs.

A list of the feature set differences between WSS and MOSS can be found at http://office.microsoft.com/en-us/sharepointtechnologyFX101758691033.aspx.

From a development perspective though, the great thing about all of these solutions is that they are built on the same core, the ASP.NET Web Parts Framework. This means that, for the most part, you can build a custom Web part using a single base class and leverage that Web part in any one of these portal solutions. It's only when you explicitly decide that you require specific WSS or MOSS platform features that you have to choose whether you want to base your Web parts off of the ASP.NET Frameworks base WebPart class, or the SharePoint base WebPart class (which itself is simply derived from ASP.NET's WebPart base class!). If you need help deciding which base class to use, the Microsoft MSDN article "Working with ASP.NET 2.0 Web Parts and Windows SharePoint Services 3.0" offers advice. See http://msdn2.microsoft.com/en-us/library/bb153523.aspx.

Building a Basic ASP.NET Web Part
Building a basic ASP.NET-based Web part is actually quite easy. To get started, create a new Class Library project and add a new Web Custom Control file to the project. This Visual Studio template provides most of the basic infrastructure you'll need to start creating the Web part.

Once your project is set, there are a few small changes you need to make to the Web Custom Control. First, to be able to reference the objects needed to create the Web part, you need to add the WebParts namespace to the class

using System.Web.UI.WebControls.WebParts;

And now that the class knows about the WebParts namespace, you need to change the base class that the Web part derives from. By default a custom Web control derives from the WebControl class, but since this is a Web part, you want it to derive from the WebPart class, which is in the WebParts namespace you added in the first step:

public class SimpleWebPartSample : WebPart

That's it! You now have a fully functioning Web part, though admittedly it doesn't provide much functionally, simply writing out the value of the Text property. You can test out the Web part by creating a Web Parts Page. As an example, you can use the Web Parts Page created by the great "Creating an ASP.NET Web Parts Page" walk-through, available on MSDN at http://msdn2.microsoft.com/en-us/library/kswx7h7e.aspx.

To test out the custom Web part, simply replace the linksPart Label with the custom Web part control as shown in Listing 1.

Deriving from the WebPart base class lets ASP.NET know that it should treat this control as a WebPart rather than a standard Web control. This means that ASP.NET will automatically add the base features of a WebPart such as the ability to change the location of the WebPart from one WebPartZont to another, adding the default control chrome, including the title and the action menu, and including this control in the PageCatelogPart. Additionally, by deriving from the WebPart base class, you now have a bunch of extra properties that you can use to control all of the basic chrome features. For example, if you didn't want to allow your Web part to be closed by the user, you could simply set the WebParts default AllowClose property to False:

<cc1:SimpleWebPartSample ID="SimpleWebPartSample1" Title="Sample Web Part" Text="This is my custom Web Part"
runat="server" AllowClose="False" />

Another great Web part feature that you get for free is the ability to let your users move parts around to different WebPartZones on your page. To see this simply add another WebPartZone control to your Web page. When you put the page in Edit mode, you can grab the custom Web parts title and drag it to the new WebPartZone.

To learn more about how this is done, see "Changing Display Modes on a Web Parts Page" at - http://msdn2.microsoft.com/en-us/library/bw5tctbb.aspx

Now that the basic Web part is in place, you can use the standard ASP.NET custom server control techniques to add functionality to the part. For example, you could create a more complex Web part that uses the Reuters news services RSS feeds to display a list of the top news stories. To do that you can modify the RenderContents method and add additional rendering logic. In this case, the method grabs the RSS feed and loops through all of the news items, generating new content for each item as shown in Listing 2.

Once the RenderContent is changed the Web part is now rendered as a more complex controls as shown in Figure 2.

Keep in mind that although this sample uses RenderContents to add the additional rendering logic using the HtmlTextWriter, as with custom WebControl creation practices, there are several means to an end. Instead of manually writing out HTML tags and attributes, you could instantiate instances of other ASP.NET controls like Label and Button, and add them to the WebParts Controls collection. In that case you'd probably want to override the Web part's CreateChildControls method and add the ASP.NET controls there.

    protected override void CreateChildControls()
    {
       base.CreateChildControls();

       Label label = new Label();
       label.Text = "Enter Search Phrase: ";

       TextBox textbox = new TextBox();

       Button button = new Button();
       button.Text = "Search";

       this.Controls.Add(label);
       this.Controls.Add(textbox);
       this.Controls.Add(button);
    }

Using this method, you can leverage pre-existing ASP.NET controls within a custom Web part.

That's all it takes to create custom ASP.NET Web parts. But what if you wanted to take this part and host it inside of the ASP.NET WebPart Framework's big brother, WSS?


More Stories By Devin Rader

Devin Rader is the product manager for Web Clients at Infragistics, the leading provider of presentation layer tools, and the author of a number of books on ASP.NET and Silverlight. You can check out his blog at www.geekswithblogs.com/Devin.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.