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

use*_*147 12 sharepoint sharepoint-2010

我使用的管理客户端对象模型在2010年SharePoint和我想要得到loginaName的的AssignedTo用户在任务列表.

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

我该如何解决这种情况?

谢谢

ekh*_*nna 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;
    }
Run Code Online (Sandbox Code Playgroud)

  • LookupValue给我显示名称而不是登录名. (8认同)

pho*_*par 9

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

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

ctx.Load(user);
ctx.ExecuteQuery();
Run Code Online (Sandbox Code Playgroud)


小智 3

您可以从列表中获取作为 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;
    }
Run Code Online (Sandbox Code Playgroud)