3

(using sp online, csom)

I want to get users LoginName the cheapest possible way (ie, avoid executequery). The user is stored in a field of type User. According to this example, the users loginname should be in res.LookupValue (below), but I get users displayname.

var userValue = (FieldUserValue)pListItem[pColumn];
string thisWillBeUsersDisplayName = userValue.LookupValue;

What am I doing wrong?

1

The FieldUserValue.LookupValue depends on how the field is configured in the list. It may return the display name, email address, login name. This can be updated at any time by someone with design permissions on the list.

You can use the FieldUserValue.Email property to call the Utility.ResolvePrincipal method to get the LoginName. See https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-user-identity-and-properties-in-sharepoint#retrieve-current-website-user-identity-by-using-the-web-object for details.

0

The FieldUserValue Class will return only the display name.

Try SPFieldUserValue

var userLookup = (SPFieldUserValue)useritems[0]["Author"];

var loginName = userLookup.LoginName;

https://social.msdn.microsoft.com/Forums/office/en-US/a4eac23a-9eb0-4284-a2c8-167c39135e0e/why-do-i-get-only-the-display-name-from-listitemauthorlookupvalue-?forum=sharepointdevelopmentprevious

  • 2
    Im using CSOM (for sharepoint online), SPFieldUserValue is server object model. Its not possible to use here – Cowborg Jul 25 '17 at 9:59
0

In CSOM FieldUserValue.LookupValue returns user's display name. Alternatively you can query user details from 'Users Info List' by passing user id but you have to do it with ClientContext.ExecuteQuery() which you want to avoid.

In case, If you want to use REST API then you can use below,

_api/web/lists/getbytitle('listname')/items(Id)?$select=YourUserField/Name&$expand=YourUserField/Id

Above endpoint url return 'Name' which is similar to Login Name in sharePoint online. REST is also easy & faster way to retrieve data.

0

In CSOM:

var userValue = (FieldUserValue)pListItem[pColumn];
var user = clientContext.Web.GetUserById(userValue.LookupId);
clientContext.Load(user, x => x.LoginName);
clientContext.ExecuteQuery();
string thisWillBeUsersLoginName = user.LoginName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.