6

Using the object model, I've retrieved the value from a people picker using standard method, and it returns a string value like this:

1#Username

I have a problem with this because I was expecting the full username, including the domain. For example:

1#MyCompanyDomain\Username

There must be a way to get the full user and domain name using the object model, but I don't know how.

2 Answers 2

9

I'm working with my PeopleEditor control this way:

foreach (var loginName in peopleEditor.CommaSeparatedAccounts.Split(';',','))
{
    SPUser user = SPContext.Current.Web.EnsureUser(loginName);
    // user.LoginName is now returns "domain\login"
}

This code works fine for me.

Update:

Note 1: EnsureUser will add the user to your site (thanks Ivan Padabed for the comment). In most cases it is ok, but better to remember this.

Note 2: This solution in most cases is most preferrable for me, than SPUtility.ResolvePrincipal. At least, because it is avaliable in Sandbox, and ResolvePrincipal isn't.

5
  • Can also use Name instead of loginName in order to get the login portion only. May 18, 2011 at 8:12
  • Please note that way you all have side effect of adding picked user to the current web! May 18, 2011 at 10:22
  • @Ivan Padabed thanks for the comment! I've updated the answer. May 18, 2011 at 11:17
  • This is awesome. Works great. What if I wanted to get the display name as well? Mar 31, 2015 at 14:25
  • user.Name should do the trick :) Mar 31, 2015 at 15:07
7

I believe you might also be able to use:

SPUtility.ResolvePrincipal(webApp, null, login, SPPrincipalType.User, SPPrincipalSource.All, false);

That said, this is slower than the suggestions using EnsureUser, but does have the benefit of not adding the user to the site, and is a bit more flexible, if you need that sort of thing.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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