How to query the Active Directory to get the Department Name, Manager Name and Team members C#.Net / .Net 4.5

.Net has the most extensive libraries readily available to query the Active Directory Services. But most of the MSDNs nowadays talk more about the Azure Services to access the Active Directory. Below, I have specified, the easiest to get the Department Name, Manager Name, Team Members of an Employee.

I am using a ASP.Net Web Service written in .Net 4.5.

STEPS

1. Create a C#.Net Web Service.

2. Add the following references to your project:

  • System.DirectoryServices
  • System.DirectoryServices.AccountManagement

3. Create a new class by name PrincipalExtension and copy the below contents into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

public static class PrincipalExtension
{
public static String GetProperty(this Principal principal, String property)
{
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(property))
return directoryEntry.Properties[property].Value.ToString();
else
return String.Empty;
}

public static String GetCompany(this Principal principal)
{
return principal.GetProperty(“company”);
}

public static String GetDepartment(this Principal principal)
{
return principal.GetProperty(“department”);
}

public static String GetManager(this Principal principal)
{
return principal.GetProperty(“manager”);
}
}

4. Go to your default Service.cs class and then call the methods from your ASP.Net web methods, as shown below:

[WebMethod]
public string GetUserTeam(string asUserName)
{
string sTeamName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

sTeamName = userPrincipal.GetDepartment();

return sTeamName;
}

[WebMethod]
public string GetUserTeamManager(string asUserName)
{
string sManagerCN = “”;
string sManagerName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

string[] sArrSeparators = new string[] { “,” };
string[] sArrStringSplit;

sManagerCN = userPrincipal.GetManager();

if (sManagerCN != “”)
{

sArrStringSplit = sManagerCN.Split(sArrSeparators, StringSplitOptions.None);

sManagerName = sArrStringSplit[0].Remove(0, 3);

return sManagerName;
}
else
{
sManagerName = “”;
return sManagerName;
}
}

[WebMethod]
public string GetUserTeamMates(string asUserName)
{

string sManagerCN = “”;

string sManagerName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

string[] sArrSeparators = new string[] { “,” };
string sConcatTeamMates = “”;

sManagerCN = userPrincipal.GetManager();

DirectorySearcher search = new DirectorySearcher();

search.Filter = “(&(objectClass=user)(manager=” + sManagerCN + “))”;
search.PropertiesToLoad.Add(“name”);

SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains(“name”))
{
sConcatTeamMates = sConcatTeamMates + “,” + (String)result.Properties[“name”][0];
}
}

if (sConcatTeamMates.Contains(“,”))
{
sConcatTeamMates = sConcatTeamMates.Remove(0, 1);
}
}

return sConcatTeamMates;

}

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s