Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

SharePoint Specialist / Author / MCT / MCTS / MCP / MSc ranting about SharePoint and other interesting Microsoft technologies

Having fun with the SharePoint Welcome.ascx control

Posted at 2009-03-25 11:33 by Wictor Wilén in SharePoint with 10 comments.

SharePoint Welcome Control Have you ever wanted to get rid of the Welcome text before the user name in the SharePoint user menu? At least I have! If you are like me - here is a solution that you can use to customize the text of the Welcome Control (Welcome.ascx).

The approach is pretty simple, and can be made in several ways, but I wanted a pretty clean solution that didn’t affect any other behavior of SharePoint and I wanted to write as little code as possible. Eventually I ended up with some more rows than expected, but this was due to the fact that I created this custom Welcome control configurable and deployable.

First of all some basics about the Welcome control. The Welcome.ascx control is a special control that handles the user menu, located top right (as standard) in your SharePoint site. The control has two states; one if the user is signed in and one when the user is not signed in. The Welcome.ascx control is located in the 12-folder under TEMPLATE\CONTROLTEMPLATES and uses the Microsoft.SharePoint.WebControls.Welcome control class in the Microsoft.SharePoint DLL file.

What I wanted to do was get rid of the Welcome text in front of the user name and only have the user’s name. To achieve this without recreating the whole control, I created a new class which derives from the Welcome control and changed the Welcome.ascx file to inherit from my new class instead.

  1: namespace ManagedWelcome {
  2:     public class Welcome : Microsoft.SharePoint.WebControls.Welcome {
  3:         protected Welcome() {
  4:         }
  5: 
  6:         [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
  7:         protected override void OnLoad(EventArgs e) {
  8:             base.OnLoad(e);
  9: 
 10:             if (HttpContext.Current.User.Identity.IsAuthenticated) {
 11:                 PostCacheSubstitutionText pt = base.ExplicitLogout.MenuControl.Controls[0] 
 12:                                                as PostCacheSubstitutionText;
 13:                 pt.TextType = PostCacheSubstitutionTextType.UserName;
 14:             }
 15:         }
 16:     }
 17: }

The original Welcome control consists of two objects; ExplicitLogout (of the type PersonalActions) and ExplicitLogin (of the type ApplicationPageLink). In line 13 I change the TextType property (enumeration of type PostCacheSubstitutionTextType) of the first control (which is of the type PostCacheSubstitutionText) in the ExplicitLogout object and set it to show only the user name (UserName enum value), default is the WelcomeUser value.

Now the Welcome control looks like this instead.

No Welcome text!

As always I cannot stop there so I made this into a customizable control, download below, which can be deployed to your web application. When activated, on Site Collection Features, it adds a link to the Site Collection Administration (not the best place, since the changes will affect the whole web application).

Site Collection Administration

When you click that link you will get to a Welcome Control administration page, where you can set the behavior of the Welcome control when a user is logged on or change the Sign In text.

Welcome Control admin

Using this feature you can on the fly change the behavior of the Welcome menu; have a custom Sign In text, change the welcome text to just the user name, the user login name, or e-mail and even the name of the Web.

Note: When installing this feature it will write over the default Welcome.ascx control, so make a backup of it first.

For full source download click here or just the solution package (WSP) click here.

Comments and trackbacks

#  Presumably you could also just create a new control? by Andy Burns
Screenshot from websnpr Rather than overwriting the Welcome.ascx, presumably you could create your own using your techniques (e.g. 'MyWelcome.ascx'), and then change your master page to point to the new one? All you'd have to do is change the tag prefix at the top of the page.
#  @Andy by Wictor
Screenshot from websnpr Hi, thats correct. But as I said in the post this is a sampl in which I tried to use as little coding as possible. But you're right, in a more complete and real world scenario that would be a preferred solution.
#  How to show just first name? by Andrew M
Screenshot from websnpr Hi Wictor, thanks for writing this up, I've found it helpful. I would like to display only the user's first name - but this isn't one of the options in PostCacheSubstitutionTextType. Can you think how I could acheive this?
#  @Andrew by Wictor
Screenshot from websnpr I would then suggest writing a completley new implementation of the Welcome control; use Reflector to work out how it works. Or, make a control adapter to make the changes.
#  RE: How to show just first name? by Andrew M
Screenshot from websnpr Wictor, thanks for your reply. As I can easily get a reference to "base.ExplicitLogout.MenuControl.Controls[0]" couldn't I determine the user's first name (using a webservice or whatever) and then just modify the text? I suspect it's not that simple! That's why I'm asking, I guess! I must admit I don't fully understand the need to use PostCacheSubstitutionText - is this because it's an easy way to get email, login etc or because it's needed for the caching to work properly? Also, I have used the PrefixHtml property to display a custom greeting before the username - does this have any implications for caching? Thanks for your help, it's appreciated.
#  long winded approach by Zubin
Screenshot from websnpr Why don't you just change the value in the resource file?
#  Sharepoint Sucks by Sharepoint
Screenshot from websnpr Sharepoint Sucks
#  Display first name only by nyalex
Screenshot from websnpr Can you add a first name only behavior please?
#  Display first name only by nyalex
Screenshot from websnpr Can you add a first name only behavior please?
#  Hi, by Amin
Screenshot from websnpr I'd deployed the webpart but it's display access denied when I change the behaviour. Anyone having the same issue? And is there any solution for that?thanks
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.

web tracker
msfeedicon The one and only feed enhancement utility for Internet Explorer 7.
Read more...