Click here to Skip to main content
13,357,973 members (57,574 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 30 May 2006

SharePoint Authenticated Content WebPart

, 30 May 2006
Rate this:
Please Sign up or sign in to vote.
A SharePoint Portal Server Webpart that can publish content depending on a user’s authentication.


A SharePoint Portal Server Webpart that can publish content depending on a user's authentication.

This tool was developed by Formation Technology, <city w:st="on" />Melbourne</city /> <country-region w:st="on" /><place w:st="on" />Australia</place /></country-region />.</strong />


A customer was running a single portal accessible via 2 URL's: one being authenticated, the other being unauthenticated. They needed a simple way to move users into an authenticated state without change the URL.

They requested a "sign-in" button for the unauthenticated user. Once authenticated, the content would then change to a personalised welcome message.


The WebPart is written for SharePoint Portal Server 2003 SP2. The WebPart requires access to the SharePoint API and so must run as a trusted assembly. It therefore is installed into the GAC to avoid security issues.

The WebPart is installed using stsadm.exe as part of the SharePoint package. Typically found in: \Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN


It is important to note that you cannot write WebParts using Visual Studio 2005 as SharePoint Portal Server has very limited compatibility with the .NET Framework 2.0.

The WebPart was written as a class library using the SharePoint SDK extension for Visual Studio 2003. The extension is a new project type for creating a WebPart library. The library will include a sample WebPart cs file, a dwp file and a manifest file. Found here:

You can download the SharePoint 2003 SDK here:

General information about the structure of a WebPart can be found here:

WebPart Properties

Only two properties are included on the WebPart. One is for entering the HTML to display when the user is authenticated and the other is for entering the HTML to display when the user is unauthenticated. There is no provision for sharing or reusing HTML between the two properties. You will just need to duplicate any common HTML.

Category("Authentication Content"),
FriendlyName("Authenticated HTML"),
Description("Insert HTML you wish to appear when the user is in 
an authenticated state.")]
public string AuthenticatedHtml
        return authenticatedHtml;
        authenticatedHtml = value;

Category("Authentication Content"),
FriendlyName("Un-Authenticated HTML"),
Description("Insert html you wish to appear when the user is in 
an unauthenticated state.")]
public string UnAuthenticatedHtml
        return unAuthenticatedHtml;
        unAuthenticatedHtml = value;

Figure 1

These two properties simply store the entered HTML as a string. Using the SharePoint properties attributes you can control how the property is presented to the administrator when adding the WebPart to the SharePoint page.

The value for the Category attribute can be anything, and does not need to be created elsewhere. FriendlyName is the name of the property as it appears in the SharePoint administration UI.


There are three placeholders you can use to add dynamic values into the authenticated HTML content. These placeholders will not work for the unauthenticated HTML (by design, not limitation).

The placeholders are:

  • %N: Replaced with the currently authenticated user's display name (i.e. Their full name)
  • %L: Replaced with the currently authenticated user's login name (i.e. ACME\Sam)
  • %E: Replaced with the currently authenticated user's email address.

Note that with data such as the display name or email address it must have first been entered into SharePoint or imported from the Active Directory.

Rendering WebPart Output

To render the WebPart output a check is first made to determine if the current user is authenticated.

If the user is not authenticated then the unauthenticated HTML content is directly outputted. If the user is authenticated, then a call is made to the SharePoint API to retrieve the instance of the current user, represented in the object SPUser. SPUser provides the Name, LoginName and Email properties that we require to replace the placeholders with the correct values. If the value is not in the SPUser then the placeholder will be removed. In its place, nothing will be inserted.

protected override void RenderWebPart(HtmlTextWriter output)
    if (Context.User.Identity.IsAuthenticated)
        SPUser myUser = SPControl.GetContextWeb(Context).CurrentUser;
        authenticatedHtml = authenticatedHtml.Replace("%N", myUser.Name);
        authenticatedHtml = authenticatedHtml.Replace("%L", myUser.LoginName);
        authenticatedHtml = authenticatedHtml.Replace("%E", myUser.Email);


Figure 2

Installing the WebPart

While there a few (manual) ways to install a webpart, the easiest is to package the part in a CAB file and use stsadm.exe to install the part. To do this create a CAB package setup project and insert the Content Files output and Primary output for your WebPart class library. This ensures your DLL, DWP and XML manifest are included in the CAB file.

Verify that the "Build Action" for the DWP file and XML manifest file are set to "Content". Do this by selecting the file in the Solution Explorer and viewing its properties in the properties window.

Build the CAB project and copy the outputted CAB file to your SharePoint server. The path to stsadm.exe is shown in the Environment section of this document. Run:

stsadm.exe –filename –globalinstall –force

The argument –globalinstall tells stsadm to insert your WebPart into the GAC and add it to the WebPart library on all instances of SharePoint on the current server. The argument –force tells stsadm to replace any already installed instances of the WebPart with the one presently in the CAB file. If there are no previous instances, this argument has no effect.

That's it!

Be sure to send us an email with your feedback.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Formation Technology
Web Developer
Australia Australia
Formation Technology is a professional services company providing innovative IT business solutions. Our team of committed Microsoft Certified Professionals are trained and dedicated to helping organisations share knowledge, communicate and deal with complex business challenges.

We have built our reputation on a no-nonsense, "on time and on budget" approach that has seen the successful deployment of: solutions that support thousands of concurrent users, portals that improve collaboration and knowledge worker productivity, and expert internet and intranet systems.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Heaven202018-Mar-13 3:08
memberHeaven202018-Mar-13 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 30 May 2006
Article Copyright 2006 by Formation Technology
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid