How can I get details like the manager name and email address from the active directory manager associated with a user?
I am able to get all details of users:
ActiveDirectory.SearchUserinAD("ads", "sgupt257");
public static bool SearchUserinAD(string domain, string username)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, domain))
{
using (var user = new UserPrincipal(domainContext))
{
user.SamAccountName = username;
using (var pS = new PrincipalSearcher())
{
pS.QueryFilter = user;
var results = pS.FindAll().Cast<UserPrincipal>();
{
foreach (var item in results)
{
File.WriteAllText("F:\\webapps\\CIS\\UserInfo.txt", item.DisplayName + item.Name + item.EmailAddress + item.EmployeeId + item.VoiceTelephoneNumber + item.Guid + item.Context.UserName + item.Sid);
}
if (results != null && results.Count() > 0)
{
return true;
}
}
}
}
}
return false;
}
Thanks.
You can get manager with something like that: DirectoryEntry dirEntry = new DirectoryEntry("LDAP://DC=company,DC=com"); DirectorySearcher search = new DirectorySearcher(dirEntry); search.
Use the Set-ADUser cmdlet to change Active Directory user attributes. To display the properties of a specific user, use the –Identity parameter. Identity can be a username, login (SAMAccountName), DN (Distinguished Name), SID, or GUID.
I use DirectorySearcher for getting data from AD. You can get manager with something like that:
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://DC=company,DC=com");
DirectorySearcher search = new DirectorySearcher(dirEntry);
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("manager");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("sAMAccountName");
if (username.IndexOf('@') > -1)
{
// userprincipal username
search.Filter = "(userPrincipalName=" + username + ")";
}
else
{
// samaccountname username
String samaccount = username;
if (username.IndexOf(@"\") > -1)
{
samaccount = username.Substring(username.IndexOf(@"\") + 1);
}
search.Filter = "(sAMAccountName=" + samaccount + ")";
}
SearchResult result = search.FindOne();
result.Properties["manager"][0];
Now you know who is manager, so you can query for data about manager.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With