Creating Web Part Appearance

The core of the web part template code is the Render method. This method is called just before the control is disposed, and it determines the appearance of the part on the page.

To create this appearance, you write HTML to the writer object. For example, the following code displays a simple table containing information about the current user's identity:

// Requires this line at class level
// using System.Security.Principal
protected override void Render (HtmlTextWriter writer)
{                    // ^ in SharePoint WebPart class this is RenderWebPart
    // Get the User identity.
    IPrincipal user = this.Context.User;
    // Write table to writer
    writer.Write("<TABLE id='tblUser'>");
    writer.Write("<TR><TD>Authenticated</TD><TD>");
    writer.Write(user.Identity.IsAuthenticated.ToString(  ));
    writer.Write("</TD></TR><TR><TD>User name</TD><TD>");
    writer.Write(user.Identity.Name);
    writer.Write("</TD></TR><TR><TD>Authentication type</TD><TD>");
    writer.Write(user.Identity.AuthenticationType);
    writer.Write("</TD></TR><TR><TD>Code is impersonating</TD><TD>");
    writer.Write(WindowsIdentity.GetCurrent(  ).Name);
    writer.Write("</TD></TR><TR><TD>Request language: </TD><TD>");
    writer.Write(this.Context.Request.UserLanguages[0]);
    writer.Write("</TD></TR><TR><TD>Request host: </TD><TD>");
    writer.Write(this.Context.Request.UserHostName);
    writer.Write("</TD></TR><TR><TD>Request IP: </TD><TD>");
    writer.Write(this.Context.Request.UserHostAddress);
    writer.Write("</TD></TR></TABLE>");
}

Tip

This example uses ...

Get Essential SharePoint 2007, 2nd Edition 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.