Pages

Monday 15 July 2013

Check if user exists in AD , SharePoint 2010

Hi All,
This requirment may come accorss many of you to check wheather a user exist in Active Directory.
The best solution which i found is to check by UserProfileManager if user exists or not, as UserProfile service is always in sync. with AD.
Namespace used:
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;

and refrence them.
you can find the dll in ISAPI folder of 14 hive.

Code:

 public bool GetUserProfile(string LoginName)
        {
            bool userexist=false;
            SPWeb currentWeb = SPContext.Current.Web;
            SPServiceContext serverContext = SPServiceContext.GetContext(currentWeb.Site);
           
            UserProfileManager profileManager = new UserProfileManager(serverContext);
            if (profileManager.UserExists(LoginName))
            {
                userexist = true;
            }
            else
            {
                userexist = false;
            }
            return userexist;

        }

 you can directly call this mehod and provide user login name as parameter ,
 it will return a bool value for you.  :)

Please share  if any other clean way to check if user exist or not.


1 comment: