| By Devin Rader | Article Rating: |
|
| January 9, 2008 10:00 AM EST | Reads: |
9,727 |
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?
Published January 9, 2008 Reads 9,727
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Practical Approaches for Optimizing Website Performance
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- WPF Controls by DevExpress
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX


































