7

I have the login ID of that user. But in the code I want to convert that login name into SPUser Object. Can I convert LoginName(type string) to SPUser. Or is there any way to convert windows identity user token to SPUser. I am adding users to a spgroup(so does not exist on the site). All I have is windows user token.

Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
sssreddy
  • 282
  • 1
  • 3
  • 15

2 Answers2

21

Take a look at SPWeb.EnsureUser. From MSDN:

"Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site."

Used thusly:

SPUser newUser = SPContext.Current.Web.EnsureUser(@"domain\logonname");
CBono
  • 3,803
  • 1
  • 32
  • 40
  • 8
    Take care with EnsureUser - it creates a new user if it doesn't find the one you are looking for – Dennis G May 12 '11 at 13:17
  • How do I make it if I need name from users that not necessarily was added to site but they are present in ActiveDirectory for example? (and I do not need that they be added to the site) – rolivares Jul 05 '12 at 15:28
  • @rolivares - You just need their display name? – CBono Jul 09 '12 at 16:06
  • Yes, I need to list all possible users (Name and ID), so I need to list users in the ActiveDirectory connected to Sharepoint, not only users having permissions to view the site. – rolivares Jul 09 '12 at 16:32
  • I mean, list all possible users, no matter if Sharepoint is connected to AD, LDAP or another authentication repository, – rolivares Jul 09 '12 at 16:57
  • @rolivares - You're out of my area of expertise. That sounds like you need to write your own query (LDAP?) into a user repository. This isn't related to this question, so I'd suggest you ask a new question either here or on sharepoint.stackexchange.com. – CBono Jul 09 '12 at 18:45
  • @CBono nops, I need list them through Sharepoint, get all possible users from repository (no matter what is). For example, when you search users in order to add them to a group, Sharepoint displays all users allowing to you choose a set and add them to the group. – rolivares Jul 20 '12 at 14:25
  • @rolivares - That sounds like you need to set up custom authentication providers for each repository. Once you do that, you can search, find and list all users, add them to groups, etc. http://blogs.msdn.com/b/russmax/archive/2010/05/27/understanding-sharepoint-2010-claims-authentication.aspx – CBono Jul 22 '12 at 19:15
9
SPUser user = SPContext.Current.Web.Users["domain\\login"];
Andrey Markeev
  • 1,344
  • 15
  • 20