Sharepoint - Get Current User's Department Name and Manager From Active Directory

The below code access current user's department and manager from AD:

    SPSite _site = SPContext.Current.Site;
   ServerContext serverContext = ServerContext.GetContext(_site);
   UserProfileManager myUserProfile = new UserProfileManager(serverContext);
   UserProfile currentUserProfile = myUserProfile .GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

   string departmentName = (string)currentUserProfile["department"].Value;
   string managerName = (string)currentUserProfile["manager"].Value;
   _site.RootWeb.Dispose();
   _site.Dispose();

The other way to do this is to use DirectoryEntry and DirectorSearcher class:

1. Get user details from Active Directory

2. How to get user data from Active Directory

3. All operation on AD using C#


Converting string to SPUser


string managerName = (string)currentUserProfile["manager"].Value;

SPFieldUserValue _spUserValue = new SPFieldUserValue(myWeb, managerName);

SPUser myUser = _spUserValue.User;

Note: myWeb is the object of SPWeb class

Example:

SPSite _site = SPContext.Current.Site;
SPWeb myWeb = _site.OpenWeb();

Hope this helps.