如何从“AssignedTo"中获取 Sharepoint User 对象字段使用客户端对象模型? [英] How to get Sharepoint User object from the "AssignedTo" field using client side object model?

查看:49
本文介绍了如何从“AssignedTo"中获取 Sharepoint User 对象字段使用客户端对象模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using managed client side object model in sharepoint 2010. And I want to get loginaName of the AssignedTo user in Task list.

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

In server side object model I use SPFieldUserValue.User.LoginName to get this property but in client side object model FieldUserValue.User does not exists.

我该如何解决这种情况?

How can I resolve this situation ?

谢谢

推荐答案

这是代码.我从任务列表中举了一个 AssignedTo 字段的例子.我希望这会有所帮助.

Here is the code for that. I've taken an example of AssignedTo field from Task list. I hope that helps.

    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;
    }

这篇关于如何从“AssignedTo"中获取 Sharepoint User 对象字段使用客户端对象模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆