Creating Web Part Appearance

The core of the web part template code is the RenderWebPart 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 output object. For example, the following code displays a simple table containing information about the current user's identity:

     ' Requires this line at class level
     ' Imports System.Security.Principal
     Protected Overrides Sub RenderWebPart _
       (ByVal output As System.Web.UI.HtmlTextWriter)
         ' Get the User identity.
         Dim user As IPrincipal = Me.Context.User
         ' Write table to output
         With output
              .Write("<TABLE id='tblUser'>")
              .Write("<TR><TD>Authenticated</TD><TD>")
              .Write(user.Identity.IsAuthenticated())
              .Write("</TD></TR><TR><TD>User name</TD><TD>")
              .Write(user.Identity.Name())
              .Write("</TD></TR><TR><TD>Authentication type</TD><TD>")
              .Write(user.Identity.AuthenticationType())
              .Write("</TD></TR><TR><TD>Code is impersonating</TD><TD>")
              .Write(WindowsIdentity.GetCurrent().Name)              .Write("</TD></TR><TR><TD>Request language: </TD><TD>")
              .Write(context.Request.UserLanguages(0))
              .Write("</TD></TR><TR><TD>Request host: </TD><TD>")
              .Write(context.Request.UserHostName)
              .Write("</TD></TR><TR><TD>Request IP: </TD><TD>")
              .Write(context.Request.UserHostAddress)
              .Write("</TD></TR></TABLE>")
         End With
     End Sub

At runtime, the preceding web part displays user information in a two-column table, as shown in Figure 9-5.

Figure 9-5. Rendering UserInfo web ...

Get Essential SharePoint 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.