【问题标题】:How to get Sharepoint User object from the "AssignedTo" field using client side object model?如何使用客户端对象模型从“AssignedTo”字段中获取 Sharepoint User 对象?
【发布时间】:2011-03-23 19:01:26
【问题描述】:

我在 sharepoint 2010 中使用托管客户端对象模型。我想在任务列表中获取 AssignedTo 用户的 loginaName

在服务器端对象模型中,我使用 SPFieldUserValue.User.LoginName 来获取此属性,但在客户端对象模型中 FieldUserValue.User 不存在。

我该如何解决这种情况?

谢谢

【问题讨论】:

    标签: sharepoint sharepoint-2010


    【解决方案1】:

    您从列表中获得作为 FieldUserValue 的列,一旦您使用查找 id 值,然后查询站点用户信息列表。在下面的示例中,我缓存了结果以防止多次查找相同的 id,因为查询可能很昂贵。

    private readonly Dictionary<int, string> userNameCache = new Dictionary<int, string>();
    public string GetUserName(object user)
    {
            if (user == null)
            {
                return string.Empty;
            }
    
            var username = string.Empty;
            var spUser = user as FieldUserValue;            
            if (spUser != null)
            {
                if (!userNameCache.TryGetValue(spUser.LookupId, out username))
                {
                    var userInfoList = context.Web.SiteUserInfoList;
                    context.Load(userInfoList);
                    var query = new CamlQuery { ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ID' /><Value Type='int'>" + spUser.LookupId + "</Value></Eq></Where></Query></View>" };
                    var users = userInfoList.GetItems(query);
                    context.Load(users, items => items.Include(
                        item => item.Id,
                        item => item["Name"]));
                    if (context.TryExecuteQuery())
                    {
                        var principal = users.GetById(spUser.LookupId);
                        context.Load(principal);
                        context.ExecuteQuery()
                        username = principal["Name"] as string;
                        userNameCache.Add(spUser.LookupId, username);
                    }
                }
            }
            return username;
        }
    

    【讨论】:

      【解决方案2】:

      这是代码。我已经从任务列表中获取了 AssignedTo 字段的示例。我希望这会有所帮助。

          public static User GetUserFromAssignedToField(string siteUrl)
          {
              // create site context
              ClientContext ctx = new ClientContext(siteUrl);
      
              // create web object
              Web web = ctx.Web;
              ctx.Load(web);
      
              // get Tasks list
              List list = ctx.Web.Lists.GetByTitle("Tasks");
              ctx.Load(list);
      
              // get list item using Id e.g. updating first item in the list
              ListItem targetListItem = list.GetItemById(1);
      
              // Load only the assigned to field from the list item
              ctx.Load(targetListItem,
                               item => item["AssignedTo"]);
              ctx.ExecuteQuery();
      
              // create and cast the FieldUserValue from the value
              FieldUserValue fuv = (FieldUserValue)targetListItem["AssignedTo"];
      
              Console.WriteLine("Request succeeded. \n\n");
              Console.WriteLine("Retrieved user Id is: {0}", fuv.LookupId);
              Console.WriteLine("Retrieved login name is: {0}", fuv.LookupValue);
      
              User user = ctx.Web.EnsureUser(fuv.LookupValue);
              ctx.Load(user);
              ctx.ExecuteQuery();
      
              // display the user's email address.
              Consol.writeLine("User Email: " + user.Email);
      
              return user;
          }
      

      【讨论】:

      【解决方案3】:

      上面的一切都对我有用,但不是:

      FieldUserValue fuv = (FieldUserValue)targetListItem["AssignedTo"];

      我用过:

      FieldUserValue[] fuv = targetListItem["AssignedTo"] as FieldUserValue[];

      【讨论】:

        【解决方案4】:

        fuv.LookupValue 可能包含显示名称,而不是登录名,所以我的建议是(假设您在代码中有 FieldUserValue - fuv(如 @ekhanna 所述):

        var userId = fuv.LookupId;
        var user = ctx.Web.GetUserById(userId);
        
        ctx.Load(user);
        ctx.ExecuteQuery();
        

        【讨论】:

        • 抱歉这不起作用:Microsoft.SharePoint.Client.ServerException occurred / The specified user 5651 could not be found.
        • 表示没有该ID的用户。确保您使用的是用户的 ID,而不是 SiteUsers 集合中用户的索引。
        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多