This post is migrated from previous hosting provider. There are still some issues with old posts. Please make a comment on this post with any issues.

Having fun with the SharePoint Welcome.ascx control

Tags: SharePoint

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.

23 Comments

  • Andy Burns said

    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.

  • Wictor said

    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.

  • Andrew M said

    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?

  • Wictor said

    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.

  • Andrew M said

    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.

  • Amin said

    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

  • Renuka said

    I could add custom text by commenting the original welcome text and adding new text. Add the following lines after line 13 above:- pt.PrefixHtml = " Your Text";

  • Wim Hill said

    Hi Wictor, I came across your nice post above and tried to create this solution in VS2010 (Create a User Control) I've copied the original welcome.ascx code and replace the Control to Inherit my code behind customwelcome.ascx.cs in there I've pasted you code sample above When testing it I get the following error: 'namespace' is not allowed here because it does not extend the class 'System.Web.UI.UserControl' Any idea how to solve this or do you van an example somewhere of a 2010 solution for this?

  • Wim Hill said

    Hi Wictor, I came across your nice post above and tried to create this solution in VS2010 (Create a User Control) I've copied the original welcome.ascx code and replace the Control to Inherit my code behind customwelcome.ascx.cs in there I've pasted you code sample above When testing it I get the following error: 'namespace' is not allowed here because it does not extend the class 'System.Web.UI.UserControl' Any idea how to solve this or do you van an example somewhere of a 2010 solution for this?

  • Nico de Jong said

    I retracted this WSP and the central admin site becomes unreachable and the editing of webparts is giving errors - I quickly deployed again - On the application page I unmarked 'Use custom behaviour' and will try again - but it would be nice to get a pointer on retracting this solution succesfully.. Many Thanks

  • Neeraj said

    This is great. Lots of stuff to learn about application pages, etc.
    Definitely liked the idea of having a configurable menu!!
    But you know what, when a client needs something like this about branding they stick to just one display type and do not keep modifying frequently.
    But as I said, I liked the idea.

    So, if you are really looking forward for a minimal code solution, I would recommend looking at my post here:
    http://neerajch.blogspot.com/2012/11/displaying-custom-user-name-in-welcome.html

    But great efforts Victor, enjoyed reading this.

Add a Comment

AWS Tracker

About Wictor...

Wictor Wilén is a Director and SharePoint Architect working at Connecta AB. Wictor has achieved the Microsoft Certified Architect (MCA) - SharePoint 2010, Microsoft Certified Solutions Master (MCSM) - SharePoint  and Microsoft Certified Master (MCM) - SharePoint 2010 certifications. He has also been awarded Microsoft Most Valuable Professional (MVP) for four consecutive years.

And a word from our sponsors...

SharePoint 2010 Web Parts in Action