|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
ASP.NET Developing Web Parts
Developing Web Parts
By: Ben Waldron
Jul. 21, 2003 12:00 AM
Web Parts are building blocks for creating modular Web sites. Microsoft SharePoint Portal Server 2003 and Windows SharePoint Services utilize Web Parts as the key component to add modular functionality and provide configurable user interfaces. As a developer, you know it's impossible to create a single interface that satisfies all users; however, Web Parts enable end users to assemble and arrange a complete Web page out of premade Web Parts. The Web Parts infrastructure allows data sharing between Web Parts to enable a connected user experience instead of stovepiped functionality. Because they are modular, Web Parts allow developers to encapsulate functionality and reuse across many different sites or, better yet, they can be posted centrally and left for users to use as they see fit. It should be no surprise that Web Parts utilize the ASP.NET platform. In fact, Web Parts are ASP.NET Server Controls, so if you are familiar with building Server Controls, then you will find it really easy to develop Web Parts. Server Controls are compiled components that perform application logic on the server, then spit out HTML and script to the browser. Before the advent of Web Parts, developers were limited to adding Server Controls to a Web page at design time, but now Web Parts allow users to add them at runtime to configure a unique interface. Another difference from Server Controls is that Web Parts take care of creating the "chrome" around the control, such as the border and title, which users can use to customize the Web Part by setting properties and applying themes. As with anything, Web Parts are as simple or as complicated as you make them. In this article I will create a simple Web Part and then build on the concepts shown to develop a more complicated one.
Developing a "Hello World" Web Part After the templates are installed, open a new project in your desired language and select Web Part Template Library. Yes, our first Web Part will be the infamous "Hello World" example, so I will name the project "HelloWorldWebPart" (see Figure 1). The project contains all the files needed to create a Web Part: a manifest, a .dwp file, and a class. The manifest is an XML file used to assist in the installation of the Web Part. The .dwp file is an XML document that captures a snapshot of the property settings for a Web Part and a reference to the assembly and class used to create it. The class is simply the ASP.NET Server Control that makes up the Web Part. The class or classes that make up the Web Part are responsible for implementing the business logic, display, and user-configurable properties of the Web Part, also known as the Tool Part. All Web Parts have a method called RenderWebPart, which is responsible for sending the HTML and script to the browser. For the "Hello World" example we just want to display some text in the Web Part, so we need to tell RenderWebPart to output only that text. This is all that is needed to display the text within a Web Part, so it is ready to be compiled and deployed. The RenderWebPart method, shown in Listing 1, is all it takes to create the "Hello World" Web Part.
/// <summary>
Developing a Functional Web Part This Standings Web Part uses two controls: a label to display text, and the DataGrid to display the standings in a table. The idea is that when the Web Part loads, it gets the data, places it in the DataGrid, and displays it within the chrome of the Web Part. All controls within a Web Part are instantiated within the CreateChildControls method (see Listing 2). The CreateChildControls method is called automatically after the Web Part is loaded. Just as with the "Hello World" Web Part, the RenderWebPart method will render the controls and send the HTML to the browser. The EnsureChild- Controls method is called prior to rendering the controls to make sure that all the controls are created before rendering them. The CreateChildControls, GetStandings, and RenderWebPart methods are important pieces of the Web Part. CreateChildControls creates all the controls necessary for the Web Part to function; GetStandings (see Listing 3) retrieves data from an XML document; and RenderWebPart (see Listing 4) outputs the resulting HTML.
Deploying Web Parts Allowing users or even members of the Administrators group to have the freedom to import new Web Parts can expose the hosting server to security threats. To reduce this risk, Web Parts must be explicitly designated as safe before they can be activated on the server. A Web Part is made safe by making entries in the SafeControls section of a Web.config file for the virtual server. The Web.config file is typically found at system_drive:\Inetpub\wwwroot\Web.config. Each SafeControl entry identifies an assembly that contains one or more Web Parts. You can list Web Part classes individually or you can specify that all Web Parts in the assembly are safe. Listing 5 is an example of the SafeControls section of a Web.config file, with tags preceding it that show how it fits into the XML hierarchy of the file. The SafeControl entry, as well as .NET Code Access Security, prevents unwanted Web Parts from being imported and executed on the server.
Once a control is made safe, it is ready to be imported and placed onto a Web Page. As previously mentioned, .dwp files are XML files that contain metadata about a Web Part. The .dwp file is used to import the Web Part so that it can be placed within a Web Part zone. It should be noted that if any changes are made to the .dwp file after the Web Part is imported, then the Web Part will have to be reimported to incorporate the changes. The following instructions detail how to add a Web Part using a .dwp file. Assuming that there are no errors within the Web Part, and it has been registered as a SafeControl, it will display on the page to which it has been added (see Figure 4).
Debugging Web Parts
The first step to debugging is to deploy a Debug version of the Web Part to the server and set a breakpoint where it is needed in the code. Next, follow the instructions below to attach the Debugger to the ASP.NET worker process: You must browse to the page that the Web Part is installed on so that the worker process can execute the code and hit the set breakpoint. After the breakpoint has been hit, debugging the Web Part is similar to debugging any other application in Visual Studio .NET. When finished, press the stop button in Visual Studio to detach the Debugger from the worker process.
Conclusion Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||