here i want to get User's Superior in AD (which is manager in AD hierarchy, see attached)
PS: you must set it at Organization Tab in User Properties in Active Directory User and Computers
C#
private static void GetManager(string _Username, string _Domain)
{
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, _Domain);
DomainControllerCollection dcc = DomainController.FindAll(context);
foreach (DomainController dc in dcc)
{
DirectorySearcher ds;
using (dc)
using (ds = dc.GetDirectorySearcher())
{
ds.Filter = String.Format("(sAMAccountName={0})", _Username);
ds.PropertiesToLoad.Add("manager");
ds.PropertiesToLoad.Add("mail");
ds.SizeLimit = 1;
SearchResult sr = ds.FindOne();
if (sr != null)
{
try
{
string sSuperior = sr.Properties["manager"][0].ToString();
int iInit = sSuperior.IndexOf("=") + 1;
int iLast = sSuperior.IndexOf(",", iInit);
sSuperior = sSuperior.Substring(iInit, iLast - iInit);
myCC += myCC + sr.Properties["mail"][0].ToString() + ";";
Console.WriteLine(sSuperior);
GetManager(sSuperior, _Domain);
}
catch (Exception)
{
ds.Dispose();
}
}
}
}
}
until then
Comments