Solved

Need To Determine Distinguished Name For An OU Using C#

Posted on 2014-11-10
5
346 Views
Last Modified: 2014-11-16
I need to be able to determine the distinguished name for an Active Directory Organizational Unit (OU) using C#. Once I have it I will then be able to insert it into the below statement which is part of a program to get all groups under an OU. Can someone please provide the code to identify the distinguished name or point to a published solution? Thanks.


    PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OUDistingushedName);


Thanks in advance.
0
Comment
Question by:zonkerman
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40433620
How are you getting the OU?

An OU is by its very nature a DN. An OU can not exists that is not a full path from the root of the domain. As you must know the path to the OU to get the OU in the first place....

How are you referencing the OU in the first place? What would you pass to the function that returns the DN in your mind?
0
 
LVL 1

Author Comment

by:zonkerman
ID: 40434258
I already know the OU I need which is in the variable assignment below for OU_ToSearchForGroupsIn.  It's a somewhat lengthy string in a specific format that I understand but I don't want my user's to have to know the format.  They know the name of the OU where the groups are, which for example in the below would be "Finance Dept"   that is all I want them to have to know.   So this is why I want to know know to derive the full formatted string value based on the OU name.  Can it be done and if so how.  I wish there were a function such as the following:  
public String GetDistinguisedNameForOU(String anOU_Name);

I can already derive the Distinguished names for AD groups under an OU but for some reason the code block I currently use won't work on an OU.


try
   {
     OU_ToSearchForGroupsIn = "OU=Finance Dept,OU=Company Apps,DC=XYZCorp,DC=com
     PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OU_ToSearchForGroupsIn);
   
    GroupPrincipal findAllGroups = new GroupPrincipal(PCTX, "*");
    PrincipalSearcher aPrincipalSearcher = new PrincipalSearcher(findAllGroups);
    foreach (var group in aPrincipalSearcher.FindAll())
    {
     try
     {
      }
    }
0
 
LVL 1

Accepted Solution

by:
zonkerman earned 0 total points
ID: 40434839
I found the answer. Basically I had to just use the DirectorySearcher with the filter shown below.  Doing this gives me the distinguished name for an OU name only.    

    String OU_Name ="Finance Dept";
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectCategory=organizationalUnit)(name=" + OU_Name + "))";

    SearchResult result = mySearcher.FindOne();

    if (result != null)
    {
       DirectoryEntry directoryObject = result.GetDirectoryEntry();
       distinguishedName = directoryObject.Properties ["distinguishedName"].Value.ToString();    
    }

This site needs more bottom line responses rather than lectures on things people are not asking for.
0
 
LVL 1

Author Closing Comment

by:zonkerman
ID: 40445501
I selected my own solution because it works and no one else provided a working solution.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40445564
Your solution is kind of OK but what happens when you have more that one OU somewhere called, for example, "Data Entry" ?

It will fail.

"This site needs more bottom line responses rather than lectures on things people are not asking for."

THAT'S why we ask questions about what you are trying to do and how you are getting there.  What this site needs is people who are willing to engage with an expert when they are asked a question. We don't ask you for more information because we have time to spare and feel like a chat, it's because we would like to provide the questioner with a foolproof, accurate solution, not something you can find on GOOGLE in five minutes that will kind of work sometimes until you put in into production and then it breaks on your users.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now