Update/Edit SharePoint user personal settings with SharePoint Object Model

Every user will have his/her personal settings for a site collection. Each user can view their details by clicking on Logged-in User link and select My Settings menu item.

Once "My Settings" selected, it redirects user to display his/her personal information. SharePoint redirects userdisp.aspx to show user details in this form : https://sitecollection/_layouts/userdisp.aspx?Force=True&ID=XXX

SharePoint stores user data in its “User Information List”. When user navigates to : https://sitecollection/_layouts/people.aspx. It looks like none other than a SharePoint list. These fields can be added or removed to suit the needs of an organization. If the User Profile service application is not enabled, when user visits the site initially, it pulls the user data from Active directory and it stores his/her information in the content database "User Information List". From next time, it checks the content database for the user info and if it finds it displays the data from the content database.

This user information can be updated using SharePoint object model.

 

1. Create an entity for User Settings to have all properties and easily extended.

public struct UserSettingsEntity

    {

        public string Url;

        public string LoginName;

        public string Title;

        public string Department;

        public string SipAddress;

        public string MobilePhone;

        public string Name;

        public string Notes;

        public string Picture;

        public string FirstName;

        public string LastName;

        public string WorkPhone;

        public string UserName;

        public string WebSite;

        public string SPSResponsibility;

        public string Office;

    }

2. Create SPSite object for site collection Url and get the SPUser to update user data.

private UserSettingsEntity userSettings = new UserSettingsEntity();

userSettings.Title = "Title"

            userSettings.Department = "Dept";

            userSettings.SipAddress = "sipAddr";

            userSettings.MobilePhone = "Mobile";

            userSettings.Name = "Name";

            userSettings.Notes = "AboutMe";

            userSettings.Picture = "https://xyz/abc.jpg";

            userSettings.FirstName = "FirstName";

            userSettings.LastName = "LastName";

            userSettings.WorkPhone = "WorkPhone";

            userSettings.UserName = "UserName";

            userSettings.WebSite = "https://xyz.com/";

            userSettings.SPSResponsibility = "AskMeAbout";

            userSettings.Office = "Office";

 using (SPSite site = new SPSite(“https://server/sitecollection”]))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    Console.WriteLine("Updating User for {0}", site.Url);

                    // Ensure to get a valid SPUser object for a given login name.

                    SPUser user = web.EnsureUser(userSettings.LoginName);

                    if (null == user)

                    {

     Console.WriteLine("Failed!");

                    }

                    else

                    {

                        Console.WriteLine("Got SPUser #{0}", user.ID);

                        try

                    {

                            user.UpdateUserInfo(userSettings);

                        }

                        catch (Exception ex)

                        {

                            Console.WriteLine("Failed.\r\nError: {0} : {1}",

                                user.ID, ex.Message);

                        }

                    }

                }

            }

 

3. Extend the SPUser by using SPUserExtension class to update user data.

/// <summary>

    /// SPUserExtension class which contains 2 extension methods.

    /// </summary>

    public static class SPUserExtension

    {

        /// <summary>

        /// Get the User Information for a particular user.

        /// </summary>

        /// <param name="user"></param>

        /// <returns></returns>

        public static SPListItem GetUserInformation(this SPUser user)

        {

            return user.ParentWeb.SiteUserInfoList.GetItemById(user.ID);

        }

        /// <summary>

        /// Update the user information with given title, department, sipAddress and Mobile Phone.

        /// </summary>

        public static void UpdateUserInfo (this SPUser user, UserSettingsEntity userSettings)

        {

            SPListItem userInfo = user.GetUserInformation();

            userInfo["JobTitle"] = userSettings.Title;

            userInfo["Department"] = userSettings.Department;

            userInfo["SipAddress"] = userSettings.SipAddress;

            userInfo["MobilePhone"] = userSettings.MobilePhone;

            userInfo["Title"] = userSettings.Name;

            userInfo["Notes"] = userSettings.Notes; // "Its about Me...";

            userInfo["Picture"] = userSettings.Picture; // "https://xyz/abc.jpg/";

            userInfo["FirstName"] = userSettings.FirstName;// "FirstName";

            userInfo["LastName"] = userSettings.LastName; // "LastName";

            userInfo["WorkPhone"] = userSettings.WorkPhone; // "WorkPhone";

            userInfo["UserName"] = userSettings.UserName; // "UserName";

            userInfo["WebSite"] = userSettings.WebSite; // "https://xyz.com/";

            userInfo["SPSResponsibility"] = userSettings.SPSResponsibility; // "SPSResponsibility";

            userInfo["Office"] = userSettings.Office; // "Office";

            userInfo.Update(); //write changes to SharePoint

        }

    }

 I have created the entity class as per fields shown in UI, and userInfo object (User Information List) shows all fields having SharePoint internal field names.