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:
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.
Description("Insert HTML you wish to appear when the user is in
an authenticated state.")]
public string AuthenticatedHtml
authenticatedHtml = value;
Description("Insert html you wish to appear when the user is in
an unauthenticated state.")]
public string UnAuthenticatedHtml
unAuthenticatedHtml = value;
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.
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 provides the
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)
SPUser myUser = SPControl.GetContextWeb(Context).CurrentUser;
authenticatedHtml = authenticatedHtml.Replace("%N", myUser.Name);
authenticatedHtml = authenticatedHtml.Replace("%L", myUser.LoginName);
authenticatedHtml = authenticatedHtml.Replace("%E", myUser.Email);
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 mycabfile.cab –globalinstall –force
–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.
Be sure to send us an email with your feedback.