Monday, April 20, 2009

Hide the Sign In link for the anonymous access user in anonymous access enabled site - Bend the Welcome.ascx - SharePoint MOSS

Lots of thing can be done by playing around the Welcome.ascx user control. I have came across one of the interesting thing on hiding the “Sign In” link for anonymous access users in the public facing internet site and thought of sharing with you.

 

Following are the two steps to implement this requirement in the supported way and its quite easy, thanks to master page and the SharePoint Application Page link control.

 

1.    Create a custom user control based on the OOB “WelCome.ascx” control. Override the “OnLoad” event and hide the “Sign In” application page link for the anonymous access user.

 

2.    Create a custom master page based on the any OOB parent master page with respect to your requirement and site definition. Render the Custom welcome control in the place of OOB welcome control.

 

 

You can find the Welcome.ascx user control under the “Control Templates” folder. Bunch of menu items are available for the authenticated user like My Settings, Sign in as different user, Log Out and Personalize the page. All these menu items are available as feature menu template and will be available only if the user was authenticated successfully. Following is the structure of the feature menu template and all the menu items are available under the ID “ExplicitLogOut”. You can see that the visibility of this Personal Actions control is false and the visibility will be made to true when the user is successfully authenticated.

 <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:MSOLayout_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:MSOWebPartPage_RestorePageDefault()"

                         MenuGroupId="300"

                         Sequence="300"

                         UseShortId="true"

                         />

       SharePoint:FeatureMenuTemplate>

      CustomTemplate>

SharePoint:PersonalActions>

 

 

The another part of the welcome user control is “ExplicitLogin” which has been rendered as the SharePoint Application Page Link as follows.

 

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

      ApplicationPageFileName="Authenticate.aspx" AppendCurrentPageUrl=true

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

 

 

This is the link which we need to concentrate for this requirement. By default this link visibility is false and will come alive when the user is not authenticated. This is what happens with the anonymous access user. When the anonymous user access the site this link is visible so that the unauthenticated user can sign in.

 

Fair enough on the post mortem of the welcome user control. Now copy this welcome user control and paste it under the Control templates folder as “CustomWelcome.ascx” control. In the “CustomWelcome.ascx” control add an In Line script and override the “OnLoad” event. In the “OnLoad” event for the unauthenticated user hide the  “ExplicitLogin” link.

 

protected override void OnLoad(EventArgs e)

    {

        //base.OnLoad(e);

        base.OnLoad(e);

        if (HttpContext.Current.User.Identity.IsAuthenticated)

        {

            this.ExplicitLogout.Visible = true;

        }

        else

        {

            this.ExplicitLogin.Visible = false;

            this.ExplicitLogin.Attributes.CssStyle.Add("display""block");

        }

 

    }

 

Now we are done with the custom welcome user control. Let us have a look on rendering it through the custom master page based on the “default.master” master page. Copy the default.master page and add the Tag prefix reference for the “CustomWelcom.ascx” control as follows in the custom master page :

 

<%@ Register TagPrefix="wssuc" TagName="CustomWelcome" src="~/_controltemplates/CustomWelcome.ascx" %>

 

Find the following entry in the master page :

 

<wssuc:Welcome id="IdWelcome" runat="server" EnableViewState="false">

                  wssuc:Welcome>

 

Replace the above entry with the following entry to replace the OOB welcome user control with your custom welcome user control :

 

<wssuc:CustomWelcome id="IdWelcome" runat="server" EnableViewState="false">

                  wssuc:Welcome>

 

Save the custom master page and use it for the public facing internet site and now “Sign In” link will not be available for the unauthenticated anonymous access user.

 

If you are aware of the whole welcome.ascx control and its structure then you can play with it for bending its behavior through custom user control. Happy customizing J

Post a Comment

Blog Popularty Partners

  ©All Right Reserved.

Back to TOP