如何使用客户端对象模型从“AssignedTo”字段获取Sharepoint用户对象?

12

我正在使用SharePoint 2010中的托管客户端对象模型。 我想获取任务列表中“AssignedTo”用户的登录名

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

我该如何解决这种情况?

谢谢

4个回答

14

这是那段代码。我以任务列表中的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;
    }

8
LookupValue 给出的是显示名称而不是登录名。 - Madhur Ahuja
如何在这里获取电子邮件 ID? - userAZLogicApps
@SaMolPP - 用户对象具有一个电子邮件属性,可用于显示电子邮件地址。请参阅此文档 - https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee537459%28v%3doffice.15%29 - ekhanna

9

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

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

ctx.Load(user);
ctx.ExecuteQuery();

抱歉,这个不起作用:Microsoft.SharePoint.Client.ServerException发生/找不到指定的用户5651。 - PeterX
这意味着没有该ID的用户。请确保使用的是用户的ID,而不是SiteUsers集合中的索引。 - pholpar

3

从列表中获取具有FieldUserValue的列,一旦获得该列,您就可以使用查找ID值,然后针对Sites用户信息列表进行查询。在下面的示例中,我缓存结果以防止多次查找相同的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;
    }

1

以上的所有内容对我都起作用,但是我使用了以下内容:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接