Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
Nice find, I'll test this thanks... –  JL01 May 18 '11 at 8:11
    
Can also use Name instead of loginName in order to get the login portion only. –  trgraglia May 18 '11 at 8:12
    
Please note that way you all have side effect of adding picked user to the current web! –  Ivan Padabed May 18 '11 at 10:22
    
@Ivan Padabed thanks for the comment! I've updated the answer. –  Andrey Markeev May 18 '11 at 11:17
add comment

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.

share|improve this answer
1  
Very useful comment, thanks! And I'd like to add the link to SPUtility.ResolvePrincipal reference: msdn.microsoft.com/en-us/library/ms458659.aspx –  Andrey Markeev May 18 '11 at 10:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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