Update User Personal Settings with the SharePoint Object Model (Guru Pratap Ketepalli)

Every user has his or her personal settings for a site collection. You can view these details by clicking the logged-in user link and then selecting My Settings.

image1

After My Settings is selected, you are redirected to display your personal information. SharePoint redirects userdisp.aspx to show user details in this form : https://sitecollection/_layouts/userdisp.aspx?Force=True&ID=XXX

image2

SharePoint stores user data in its “UserInfo” table in the Content DB of the site. When you navigate to : https://sitecollection/_layouts/people.aspx, it appears as 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 you visit the site initially, your data is retrieved from the Active directory and stored in the content database "User Information List". Then the next time, the content database is searched for your information, and if found, it is then displayed.

This information can be updated by using the SharePoint object model as shown in the following steps.

1. Create an entity for User Settings that has all of the properties that can be 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 a SPSite object for the site collection Url and retrieve 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";

SPSecurity.RunWithElevatedPrivileges(delegate

{

using (SPSite site = new SPSite(commandLineArguments["Url"]))

{

SPWeb web = site.RootWeb;

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 : Not a valid user");

}

else

{

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

try

{

Console.WriteLine("Attempting to update User Information for SPUser #{0}:", user.ID);

user.UpdateUserInformation(userSettings);

Console.WriteLine("Success : Updated User Information Successfully!.");

}

catch (Exception ex)

{

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

user.ID, ex.Message);

}

}

}

});

3. Extend the SPUser by using SPUserExtension class to update the 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

}

}

This code creates the entity class as per fields shown in the UI, and the userInfo object (User Information List) displays all of the fields having SharePoint internal field names.

Technorati Tags: Guru Pratap Ketepalli