Sunday, August 31, 2014

How to change the user display name in SharePoint

I have one user whose name enter wrongly in Active Directory and that user display name showing as wrongly. The user  information has been changed in the Active Directory and AD synchronising appears to be running every night.

But still user display name showing incorrectly.

I have worked on this couple of days and i got two solutions for this.

Solution 1 - Using Database

http://msdn.microsoft.com/en-us/library/dd588089%28office.11%29.aspx

Generally users info of a web application saved in UserInfo table of respective content database.


Here the query to get user details using login name



Here ukreddy is the domain and uday is the user login name

Now we are going to update user display name i.e. tp_Title is the display name in SharePoint

now user display name changed




Solution 2 using SharePoint object model:

Here the method to update user information

        public static void UpdateUserInfo(int userId, string basepath, string displayName)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(basepath));
            SPSiteCollection siteCollections = webApp.Sites;
            foreach (SPSite siteCollection in siteCollections)
            {
                try
                {
                    using (SPWeb web = siteCollection.RootWeb)
                    {
                        var user = web.SiteUsers.Cast<SPUser>().FirstOrDefault(u => u.ID == userId);
                        if (user != null)
                        {
                            if (displayName.Length > 0)
                            {
                                user.Name = displayName;
                                user.Update();
                            }
                        }
                    }
                }
                finally
                {
                    if (siteCollection != null)
                    {
                        siteCollection.Dispose();
                    }
                }
            }
        }

After changing through code here the screen shot for updated user display name

No comments:

Post a Comment