Banner Advert
Liam Cleary
Posted on July 11, 2012 by
1 Comment

Recently while answering a few questions I got faced with the need to use a specific claim in the welcome control. As you may all be aware sometimes when using Membership and Role Provider or custom Identity Providers the welcome display name can get a little messed up and not display the correct details, this is true more so with Identity Providers such as ADFS where the primary claim is used in the control. I decided that I wanted to create a custom welcome control that would allow me in code to set what the display test should be so something like: “Welcome: {Selected Claim}”.


This post will show some basic code for doing this. Firstly create a new solution and SharePoint Project. I always use the “Empty SharePoint Project”. For this example I called the solution “Helloitsliam” and the project “Helloitsliam.WelcomeControl”.




Now we have the project we need to right click and select to add a new user control.



I named my control “HelloitsliamWelcome.ascx”.



We then need to add the following code to the “ASCX” markup:



We also need to add some other markup that use “<asp:Panel>” controls for hiding and showing the relevant controls.



These panels will be for hiding and showing the correct logic depending on whether it is some sort of forms based login or windows login. So now we need to add the following base code to the panels:


 <asp:Panel runat="server" ID="idpHelloitsliamPanel" style="display:inline;">

<SharePoint:PersonalActions accesskey="<%$Resources:wss,personalactions_menu_ak%>" ToolTip="<%$Resources:wss,open_menu%>" runat="server" id="ExplicitLogout" Visible="false">

<CustomTemplate>

<SharePoint:FeatureMenuTemplate runat="server"

FeatureScope="Site"

Location="Microsoft.SharePoint.StandardMenu"

GroupId="PersonalActions"

id="ID_PersonalActionMenu"

UseShortId="true"

>

<SharePoint:MenuItemTemplate runat="server" id="ID_PersonalInformation"

Text="<%$Resources:wss,personalactions_personalinformation%>"

Description="<%$Resources:wss,personalactions_personalinformationdescription%>"

MenuGroupId="100"

Sequence="100"

ImageUrl="/_layouts/images/menuprofile.gif"

UseShortId="true"

/>

<SharePoint:MenuItemTemplate runat="server" id="ID_LoginAsDifferentUser"

Text="<%$Resources:wss,personalactions_loginasdifferentuser%>"

Description="<%$Resources:wss,personalactions_loginasdifferentuserdescription%>"

MenuGroupId="200"

Sequence="100"

UseShortId="true"

/>

<SharePoint:MenuItemTemplate runat="server" id="ID_RequestAccess"

Text="<%$Resources:wss,personalactions_requestaccess%>"

Description="<%$Resources:wss,personalactions_requestaccessdescription%>"

MenuGroupId="200"

UseShortId="true"

Sequence="200"

/>

<SharePoint:MenuItemTemplate runat="server" id="ID_Logout"

Text="<%$Resources:wss,personalactions_logout%>"

Description="<%$Resources:wss,personalactions_logoutdescription%>"

MenuGroupId="200"

Sequence="300"

UseShortId="true"

/>

<SharePoint:MenuItemTemplate runat="server" id="ID_PersonalizePage"

Text="<%$Resources:wss,personalactions_personalizepage%>"

Description="<%$Resources:wss,personalactions_personalizepagedescription%>"

ImageUrl="/_layouts/images/menupersonalize.gif"

ClientOnClickScript="javascript:ChangeLayoutMode(true);"

PermissionsString="AddDelPrivateWebParts,UpdatePersonalWebParts"

PermissionMode="Any"

MenuGroupId="300"

Sequence="100"

UseShortId="true"

/>

<SharePoint:MenuItemTemplate runat="server" id="ID_SwitchView"

MenuGroupId="300"

Sequence="200"

UseShortId="true"

/>

<SharePoint:MenuItemTemplate runat="server" id="MSOMenu_RestoreDefaults"

Text="<%$Resources:wss,personalactions_restorepagedefaults%>"

Description="<%$Resources:wss,personalactions_restorepagedefaultsdescription%>"

ClientOnClickNavigateUrl="javascript:SP.SOD.execute('browserScript', 'MSOWebPartPage_RestorePageDefault')"

MenuGroupId="300"

Sequence="300"

UseShortId="true"

/>

</SharePoint:FeatureMenuTemplate>

</CustomTemplate>

</SharePoint:PersonalActions>

<SharePoint:ApplicationPageLink runat="server" id="ExplicitLogin"

ApplicationPageFileName="Authenticate.aspx" AppendCurrentPageUrl=true

Text="<%$Resources:wss,login_pagetitle%>" style="display:none" Visible="false" />

</asp:Panel>

<asp:Panel runat="server" ID="windowsHelloitsliamPanel" style="displa:inline;">

<wussuc:Welcome id="wExplicitLogout" runat="server" />

</asp:Panel>


Now to add some “C#” code that will tie it all together. First we need to add a reference to “Microsoft.IdentityModel” so we can grab the current claims for the user. Now we need to add the following code to the “Page_Load” event of the user control. We need to check if the current login process is using Windows or not. If it is windows then we show or hide the appropriate panel.



login method is something other than windows then we can use the following custom code. We first check that the user is actually authenticated, then validate that it is not a post back.



Next we grab the “IClaimsPrinciple” for the current user, check if it is “null” and then if okay, grab the current identity. Next we grab the claims from the current user object and grab a specific claim in this case we are using “http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name“.



Now need to mess around with the “PostCacheSubstitutionText” object for the welcome control. Using this method we overwrite the current rendering and have it output “Welcome {Claim}“.



Finally we add a check at the end and modify the rendering slightly if the user is not authenticated yet.



So we need to package the component now and deploy. We then need to check that it has been deployed, for this demo I validated this by checking here:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES



Now modify the master page for the site and make the following changes to the master page, by replacing the current link to the welcome control to the custom one that has been deployed.



Now when we access the site the welcome control is rendering the following in my example:



Of course we could use any of the claims that are being sent to the server or even concatenate them to make something else. Using the claims viewer web part from the previous post I could use of the following:




This article was originally posted here, on the Hello It’s Liam blog.

One Comment

Shabih on October 17, 2012

Great Work.

Add a comment