5

How the "department name" and "manager" attribute value could be retrieved from active directory programmatically? For ex. i will have custom newform.aspx to create some new item, in the code-behind of the custom newform.aspx i have to send email to the manager of the current user, and log the department name. So sharepoint designer activities is not suitable for my case.

2 Answers 2

11

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.

3
  • thanks @zakir hc, one more thing: what is the return value of currentUserUserProfile["manager"].Value ? Can i cast it to SPUser object? So that i can get the email of manager..
    – ozdogan
    Mar 24, 2013 at 16:49
  • 1
    Hi ozdogan, the return value will be string and yes you can cast it to SPUser. Please check my above edited answer.
    – Zakir HC
    Mar 25, 2013 at 9:45
  • How to do this in Client Object Model (javascript)? Jun 17, 2015 at 11:37
0

Kindly note that ServerContext Class has been deprecated and replaced with SPServiceContext below is the updated Code

 using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url) )
                                {
                                        SPServiceContext context = SPServiceContext.GetContext(site);
                                        UserProfileManager profileManager = new UserProfileManager(context);

                                        UserProfile currentUserProfile = profileManager.GetUserProfile(item.ClaimID);
                                        item.Department = Convert.ToString(currentUserProfile[PropertyConstants.Department].Value);
                                        item.Email =Convert.ToString(currentUserProfile[PropertyConstants.WorkEmail].Value) ;
                                        //string managerName = (string)currentUserProfile["manager"].Value;

                                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.