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.
How to get SPUser Object using user id (domain_name\userid)
Asked Answered
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");
Take care with EnsureUser - it creates a new user if it doesn't find the one you are looking for –
Lilli
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) –
Eudoxia
@Eudoxia - You just need their display name? –
Cancellation
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. –
Eudoxia
I mean, list all possible users, no matter if Sharepoint is connected to AD, LDAP or another authentication repository, –
Eudoxia
@Eudoxia - 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. –
Cancellation
@Cancellation 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. –
Eudoxia
@Eudoxia - 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. blogs.msdn.com/b/russmax/archive/2010/05/27/… –
Cancellation
SPUser user = SPContext.Current.Web.Users["domain\\login"];
I am adding users to a group. So he dose not exist on the site. –
Thee
so use EnsureUser, as described in CBono post –
Questa
© 2022 - 2024 — McMap. All rights reserved.