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.