354 13.5 Coding Web Parts
ever, when developing more complex Web Parts, the underlying develop-
ment steps that you need to follow to create and deploy the Web Part remain
the same. Now that we understand the basics of Web Part development, let’s
move on to building slightly more complex Web Parts.
13.5 Coding Web Parts
When working with Web Parts, it helps to understand how a Web Part is
actually rendered. The ASP.NET 2.0 Web Part framework, and in particular
the
SPWebPartManager control, is at the heart of SharePoint’s rendering
process. The
SPWebPartManager control overrides the standard ASP.NET
2.0
WebPartManager control and allows data to be stored in the SharePoint
content database instead of the ASP.NET services database. When a request
is made for a Web Part page, SharePoint retrieves the page source. If the
code has not been customized using for example, SharePoint Designer
2007, it is considered an uncustomized (ghosted) page, and the source is
initially retrieved directly from the SharePoint directory on the Web server.
If the page has been customized, the source is retrieved from the content
database.
13.5.1 Web Part Rendering Process
The rendering process for an individual Web Part can be broken down by
examining the sequence of ASP.NET events executed throughout the pro-
cess (Figure 13.45). When a Web Part page is requested, the
OnInit event is
triggered. The
OnInit event launches the initialization phase for the Web
Part. During the initialization phase, the Web Part framework retrieves the
property values associated with the Web Part from the database, such as the
title of the Web Part and its height and width. If your custom Web Part
needs to perform any additional initialization steps, you can override the
OnInit event. If this is the first time the Web Part is loaded, it proceeds to
the
OnLoad event, which sets the Web Part properties with the values
retrieved from the initialization phase. If the Web Part has been loaded pre-
viously, the
LoadViewState method is invoked to load the state informa-
tion previously serialized into the Web Part, after which the
Onload event
proceeds and updates the properties. You can leverage the
OnLoad event to
retrieve information from various data sources, such as a database, that you
want to display in the Web Part. If your Web Part uses any ASP.NET 2.0
controls, the
EnsureChildControls and CreateChildControls events are
launched to ensure that all the appropriate server controls are initialized and
created in the control tree. Controls added by overriding the
CreateChild-
Controls
method are automatically rendered by the framework and do not
require further processing in the subsequent rendering events.
13.5 Coding Web Parts 355
Chapter 13
Once the Web Part has finished creating the required server controls and
has handled user-generated events, it is ready to render the output to the
browser. The rendering phase commences with the
OnPreRender event.
Developers can use the
OnPreRender event to change Web Part properties
before the output is rendered to the browser. On completion of the
OnPreRender event, the view state information of the Web Part is serialized
and saved to a hidden variable on the Web page for future load requests. At
this stage of the rendering process, the output can actually be rendered to the
browser. Two rendering methods, or events, are responsible for producing the
final output: the
Render event, and the RenderContents event. The Render
event is responsible for rendering the entire Web Part, including the chrome,
whereas the
RenderContents event is only responsible for rendering the
contents of the Web Part.
13.5.2 Web Part Properties and Methods
So far, we know that a Web Part is an ASP.NET control that inherits its prop-
erties and methods from the ASP.NET 2.0
WebPart base class. The proper-
ties associated with a Web Part determine the appearance and behavior of the
Web Part, and each property typically has a get and set accessor method
defined. The get accessor enables you to retrieve the value of a property, and
the set accessor enables you to set the value of a property. Figure 13.46 shows
sample C# code to define a custom Web Part property. If you have experi-
ence developing Web Parts for Windows SharePoint Services 2.0 and Share-
Point Portal Server 2003, you will notice some similarities in the process of
defining custom properties; you will also notice some differences in the
attribute terminology. For example, the
WebBrowsable attribute determines
whether the Web Part property appears as a property within the Web Part
tool pane; the same attribute was named
Browsable in the previous version
of SharePoint.
Figure 13.45
Web Part
rendering process.
Get Microsoft SharePoint 2007 Technologies now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.