Programmatic user search in SharePoint

Ive encountered a situation where I need to be able to search for a user by the name of the active directory. Ive tried to find the user in the properties of SPSite.Users, SPSite.AllUsers and SPSite.SiteUsers, but the user cannot be in any of these lists. For users who have received permission to a site through an active directory group that has been granted permission to the site, there is no listing for the user in the list of users, this is only an entry for the active directory group. Is there a way to find a user programmatically in SharePoint without directly querying the active directory through LDAP?

Ideally, I would like to find a mechanism that works for forms-based authentication and AD authentication, but I need to find a mechanism that works with AD authentication.

+4
source share
1 answer

Try SPWeb.EnsureUser :

Checks if the specified login belongs to a valid user of the website, and if the user name does not already exist, adds it to the website

This method works with both AD and FBA. If you use both options, you will need a username prefix from a non-default provider, something like this:

SPUser webUser = web.EnsureUser("SecondaryProvider:" + userName); 
+3
source

All Articles