Thursday, August 27, 2009

Custom Welcome User message in SharePoint

By-default SharePoint display Logged-in user name as --- Welcome UserName,
also its displays a context menu on mouse click which performs following actions:
a) Personal Settings
b) Sign Out
c) Sign in as different user, etc

We can add / delete menu items in Welcome User's context menu.

Now consider a scenarion where we don't want context menu, just User name and Sign-Out link are needed.

To acheive above requirement, we can either create a custom WebPart or a custom UserControl.
I preferred custom UserControl, because it easy to create and deploy.

Following is the code snippet of custom UserControl (name MyWelcomeUser.ascx) -



<%@ Control Language="C#" AutoEventWireup="true" CompilationMode="Always"
EnableViewState="true" %>

<div id="divMain" runat="server" style="width: 100%;">
<table cellpadding="0" cellspacing="0">
<tr class="column_title_link tab-bg">
<td class="welcome-text">
Welcome
<asp:Label ID="LabelUserName" runat="server"
Style="padding-left: 5px; font-weight: bold;"></asp:Label>
</td>
<td>
|
</td>
<td>
<asp:HyperLink NavigateUrl="/_layouts/signout.aspx"
ID="HyperLinkSignOut" runat="server"
Text="Logout"></asp:HyperLink>
</td>
</tr>
</table>
</div>

<script language="C#" runat="server">

protected void Page_Load(Object sender, EventArgs e)
{
LabelUserName.Text = Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.Name;
}

</script>




In the above code snippet I created a Label, whose text is set to current logged-in user in Page_Load event and a link to do Sign Out.
Here I am using SharePoint's default signout page.


Following is the snapshot of custom Login control...

1 comment:

WiLL said...

Hi

I use your custom welcome message, and when i click on the logout link, and then i enter into de web again, iam login. i think that with a link to the logout page is not enough...

any ideas?

thanks

Google