Microsoft Knowledge Base Email Alertz

How to change the display name of FBA user in Welcome control

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 970787 - Last Review: April 29, 2009 - Revision: 1.0

How to change the display name of FBA user in Welcome control

Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Symptom



When you have a site that is form authentication enabled, you'll by default get to see the account name of the logged in user in the SharePoint Welcome control.  For e.g., if the form auth account name is: mossldapmember:test1, you'll get to see "test1" in the welcome control.  Many a times this isn't preferred and customer would like to have full name displayed in the welcome control.

Cause



This behavior is observed because welcome control reads the "Name" property from the content database, which by default on a form auth enabled site is set to the account name.

Resolution



To change this and to have the full user name displayed, the following sample code will help to change the name property.  Below is a sample.  The web part has a button in it that will actually perform the change.  However, we can modify the web part to automatically run when the user visits the page for the first time and make it hidden so that the change appears seamless.

 

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using System.DirectoryServices;

using System.IO;

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

 

namespace FBANameChanger

{

    [Guid("8d557e7e-a7ef-45e7-a91c-386faf6c0389")]

    public class ChangeFbaName : System.Web.UI.WebControls.WebParts.WebPart

    {

        public ChangeFbaName()

        {

        }

 

        Button btn = null;

 

        protected override void CreateChildControls()

        {

            btn = new Button();

            btn.Text = "Click to change...";

            btn.Click +=new EventHandler(btn_Click);

            Controls.Add(btn);

        }

 

        public override void RenderControl(HtmlTextWriter writer)

        {

            writer.Write("<table><tr><td>");

            btn.RenderControl(writer);

            writer.Write("</td></tr></table>");

        }

 

        private static string ReturnADEmail(string accntName)

        {

            string email = string.Empty;

            DirectoryEntry de = null;

            DirectorySearcher ds = null;

            try

            {

                de = new DirectoryEntry("", "sr", "sr", AuthenticationTypes.Secure);

                ds = new DirectorySearcher(de);

                ds.Filter = string.Format("(sAMAccountName={0})", accntName);

                SearchResult sr = ds.FindOne();

                if (sr != null)

                {

                    de = sr.GetDirectoryEntry();

                    email = de.Properties["mail"].Value.ToString();

                }

                else

                {

                    // If nothing found handle that case here

                }

            }

            catch (Exception e)

            {

                // Exception handling mechanism

            }

            finally

            {

                if (ds != null)

                    ds.Dispose();

                if (de != null)

                    de.Dispose();

            }

            return email;

        }

 

        private static string ReturnADDisplayName(string accntName)

        {

            string fullName = string.Empty;

            DirectoryEntry de = null;

            DirectorySearcher ds = null;

            try

            {

                de = new DirectoryEntry("", "sr", "sr", AuthenticationTypes.Secure);

                ds = new DirectorySearcher(de);

                ds.Filter = string.Format("(sAMAccountName={0})", accntName);

                SearchResult sr = ds.FindOne();

                if (sr != null)

                {

                    de = sr.GetDirectoryEntry();

                    fullName = de.Properties["cn"].Value.ToString();

                }

                else

                {

                    // If nothing found handle that case here

                }

            }

            catch (Exception e)

            {

                // Exception handling mechanism

            }

            finally

            {

                if (ds != null)

                    ds.Dispose();

                if (de != null)

                    de.Dispose();

            }

            return fullName;

        }       

 

        private void btn_Click(object sender, EventArgs e)

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                string identity = Page.User.Identity.Name;

                string loginName = "mossdcLdapMembers:" + identity;

                string displayName = ReturnADDisplayName(identity);

                string email = ReturnADEmail(identity);

                SPWeb web = SPContext.Current.Web;

                web.AllowUnsafeUpdates = true;

                try

                {

                    SPUser user = web.AllUsers[loginName];

                    user.Name = displayName;

                    user.Email = email;

                    user.Update();

                    Page.Response.Redirect(web.Site.Url.ToString());

                }

                catch (UnauthorizedAccessException _unauthorized)

                { Page.Response.Redirect(web.Site.Url.ToString()); }

                catch (Exception _e)

                { }

            });

        }

    }

}

In this example, form authentication has been configured to use LDAP membership/role providers.  If you are using AspNetSqlMembershipProvider or your own custom provider, you’ll need to change this code accordingly.  For example, you might have to open a SQLConnection to your custom user store (e.g., the database that your custom membership provider is configured to use) and fetch the Display Names from the database.

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

APPLIES TO
  • Microsoft Windows SharePoint Services 3.0
  • Microsoft Office SharePoint Server 2007
Keywords: 
kbrapidpub kbnomt KB970787
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate