用SharePoint对象模型更新用户个人设置(Guru Pratap Ketepalli)

Tags: No Tags
Comments: No Comments
Published on: 2011 年 12 月 14 日

[原文发表地址] Update User Personal Settings with the SharePoint Object Model (Guru Pratap Ketepalli)

[原文发表时间] 2011-06-23  14:43

每个用户对其网站集都有自己的个人设置。你可以点击登录用户链接,选择My Settings查看详情。

clip_image002[8]

选中My Settings之后,你会被引导到显示你的个人信息。SharePoint重定位userdisp.aspx以下列形式显示用户详情:http://sitecollection/_layouts/userdisp.aspx?Force=True&ID=XXX

clip_image004[4]

SharePoint在站点Content DB中的“UserInfo”表中储存用户数据。当你导航至:http://sitecollection/_layouts/people.aspx,它就会以SharePoint列表形式显现。这些字段可以添加或删除,以适应团队的需要。如果你初次访问网站时,用户文件服务应用不可用,你的数据则会在Active目录中被检索,并储存在目录数据库“用户信息列表”中。于是下次登录时,目录数据库会搜索你的信息,如果找到,就会显示出来。

通过以下的步骤,用SharePoint对象模型可以实现信息的更新。

1. 为拥有所有易扩展属性的用户设置创建实体。

   1: public struct UserSettingsEntity

   2:  

   3: {

   4:  

   5: public string Url;

   6:  

   7: public string LoginName;

   8:  

   9: public string Title;

  10:  

  11: public string Department;

  12:  

  13: public string SipAddress;

  14:  

  15: public string MobilePhone;

  16:  

  17: public string Name;

  18:  

  19: public string Notes;

  20:  

  21: public string Picture;

  22:  

  23: public string FirstName;

  24:  

  25: public string LastName;

  26:  

  27: public string WorkPhone;

  28:  

  29: public string UserName;

  30:  

  31: public string WebSite;

  32:  

  33: public string SPSResponsibility;

  34:  

  35: public string Office;

  36:  

  37: }

2. 为网站集Url创建SPSite对象,检索SPUser以更新用户数据。

   1: private UserSettingsEntity userSettings = new UserSettingsEntity();

   2:  

   3: userSettings.Title = "Title"

   4:  

   5: userSettings.Department = "Dept";

   6:  

   7: userSettings.SipAddress = "sipAddr";

   8:  

   9: userSettings.MobilePhone = "Mobile";

  10:  

  11: userSettings.Name = "Name";

  12:  

  13: userSettings.Notes = "AboutMe";

  14:  

  15: userSettings.Picture = "http://xyz/abc.jpg";

  16:  

  17: userSettings.FirstName = "FirstName";

  18:  

  19: userSettings.LastName = "LastName";

  20:  

  21: userSettings.WorkPhone = "WorkPhone";

  22:  

  23: userSettings.UserName = "UserName";

  24:  

  25: userSettings.WebSite = "http://xyz.com/";

  26:  

  27: userSettings.SPSResponsibility = "AskMeAbout";

  28:  

  29: userSettings.Office = "Office";

  30:  

  31: SPSecurity.RunWithElevatedPrivileges(delegate

  32:  

  33: {

  34:  

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

  36:  

  37: {

  38:  

  39: SPWeb web = site.RootWeb;

  40:  

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

  42:  

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

  44:  

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

  46:  

  47: if (null == user)

  48:  

  49: {

  50:  

  51: Console.WriteLine("Failed : Not a valid user");

  52:  

  53: }

  54:  

  55: else

  56:  

  57: {

  58:  

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

  60:  

  61: try

  62:  

  63: {

  64:  

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

  66:  

  67: user.UpdateUserInformation(userSettings);

  68:  

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

  70:  

  71: }

  72:  

  73: catch (Exception ex)

  74:  

  75: {

  76:  

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

  78:  

  79: user.ID, ex.Message);

  80:  

  81: }

3. 用SPUserExtension类扩展SPUser来更新用户数据

   1: /// <summary>

   2:  

   3: /// SPUserExtension class which contains 2 extension methods.

   4:  

   5: /// </summary>

   6:  

   7: public static class SPUserExtension

   8:  

   9: {

  10:  

  11: /// <summary>

  12:  

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

  14:  

  15: /// </summary>

  16:  

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

  18:  

  19: /// <returns></returns>

  20:  

  21: public static SPListItem GetUserInformation(this SPUser user)

  22:  

  23: {

  24:  

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

  26:  

  27: }

  28:  

  29: /// <summary>

  30:  

  31: /// Update the user information with given title, department, 

  32:  

  33: /// sipAddress and Mobile Phone.

  34:  

  35: /// </summary>

  36:  

  37: public static void UpdateUserInfo (this SPUser user, 

  38:  

  39: UserSettingsEntity userSettings)

  40:  

  41: {

  42:  

  43: SPListItem userInfo = user.GetUserInformation();

  44:  

  45: userInfo["JobTitle"] = userSettings.Title;

  46:  

  47: userInfo["Department"] = userSettings.Department;

  48:  

  49: userInfo["SipAddress"] = userSettings.SipAddress;

  50:  

  51: userInfo["MobilePhone"] = userSettings.MobilePhone;

  52:  

  53: userInfo["Title"] = userSettings.Name;

  54:  

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

  56:  

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

  58:  

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

  60:  

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

  62:  

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

  64:  

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

  66:  

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

  68:  

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

  70:  

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

  72:  

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

  74:  

  75: } 

  76:  

  77: }

此代码如UI中显示的每个域一样创建实体类,userinfo对象(用户信息列表)显示了所有含有SharePoint内部域名的域。

No Comments - Leave a comment

Leave a comment


Welcome , today is 星期四, 2017 年 06 月 22 日