O'Reilly logo

Essential SharePoint by Jeff Webb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required