12
votes

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

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 ?

Thanks

4

4 Answers

14
votes

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

The fuv.LookupValue may contain the display name, not the login name, so my suggestion is (assuming you have the FieldUserValue - fuv in code (as descibed by @ekhanna):

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

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

You get the column which as the FieldUserValue from the list, once you have that you use the lookup id value and then query against the Sites User Info List. In the example below I cache the results to prevent looking up the same id more than once since the query can be expensive.

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
votes

Everything above worked for me, but instead of:

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

I used:

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