How to get the current user’s OU (Organizational Unit) from AD (Active Directory) – C#

There are so many ways to get the user details form AD (Active directory), but I wanted to get the current user’s OU (Organizational Unit) from Active directory. There is no any direct method to get the OU (Actually I could not find anything on the web, if anyone get an easy way to find-out please let me know). I am currently developing a Custom Search for SharePoint 2007 and according to the OU, I want to display the search results.

Here is the method I have created and most of the comments I have put in the code itself. I used Asp.net and C#.

public string GetOU(string username)
 {
 string result = string.Empty;
 using (HostingEnvironment.Impersonate())
 {
 //Getting the domain
 PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

//Finding the user
 UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);

//If the user found
 if (user != null)
 {
 // Getting the DirectoryEntry
 DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry);
 //if the directoryEntry is not null
 if (directoryEntry != null)
 {
 //Getting the directoryEntry's path and spliting with the "," character
 string[] directoryEntryPath = directoryEntry.Path.Split(',');
 //Getting the each items of the array and spliting again with the "=" character
 foreach (var splitedPath in directoryEntryPath)
 {
 string[] eleiments = splitedPath.Split('=');
 //If the 1st element of the array is "OU" string then get the 2dn element
 if (eleiments[0].Trim() == "OU")
 {
 result = username + "-" + eleiments[1].Trim();
 break;
 }
 }
 }
 }
 }
 return result;
 }

Please don’t forget to add the following references:

using System.Collections;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.Security.Principal;

If you want to get the current user’s OU then you can call this method in the Page_Load event.

protected void Page_Load(object sender, EventArgs e)
{
string userName = Context.User.Identity.Name;
LabelOU.Text = GetOU(userName);
}

You will get the output like this:

Domain-Name\RajanihanthV
OU-Name
I hope this small C# code will be helping someone! Thanks R./

References:

1. http://stackoverflow.com/questions/637486/how-to-get-the-current-users-active-directory-details-in-c-sharp

2. http://stackoverflow.com/questions/5309988/how-to-get-the-groups-of-a-user-in-active-directory-c-asp-net

Advertisements

, , , , , , , , , , , ,

  1. #1 by Methai on February 22, 2012 - 4:28 pm

    Can I get the code in text mode? thx

  2. #2 by Rajanihanth on February 23, 2012 - 12:55 am

    I will update the code soon. Thanks.

  3. #3 by Rajanihanth on February 24, 2012 - 10:43 pm

    public string GetOU(string username)
    {
    string result = string.Empty;
    //Getting the domain
    PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);

    //Finding the user
    UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);

    //If the user found
    if (user != null)
    {
    // Getting the DirectoryEntry
    DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry);
    //if the directoryEntry is not null
    if (directoryEntry != null)
    {
    //Getting the directoryEntry’s path and spliting with the “,” character
    string[] directoryEntryPath = directoryEntry.Path.Split(‘,’);
    //Getting the each items of the array and spliting again with the “=” character
    foreach (var splitedPath in directoryEntryPath)
    {
    string[] eleiments = splitedPath.Split(‘=’);
    //If the 1st element of the array is “OU” string then get the 2dn element
    if (eleiments[0].Trim() == “OU”)
    {
    result = username + “
    ” + eleiments[1].Trim();
    break;
    }
    }
    }
    }
    return result;
    }

  4. #4 by Leonard Marks on July 17, 2012 - 1:59 pm

    great post

  5. #5 by Anonymous on August 3, 2012 - 7:50 pm

    Hi rajanihanth, how do i get the all the OU from the AD? Do you have any idea? Please reply.

  6. #6 by aerospace engineering internship on January 22, 2013 - 12:37 am

    Thanks for the auspicious writeup. It if truth be told used to be a amusement account it.
    Look complicated to far delivered agreeable from you! However,
    how can we keep up a correspondence?

  1. The application attempted to perform an operation not allowed by the security policy – DirectoryServices « Step Bi Step

Please 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

%d bloggers like this: