locked
How to retrieve OU from active directory RRS feed

  • Question

  • Hi,

    How can i retrieve all Organization Units (OU) from active directory. i am creating webpart to list all OUs from AD.

    Thx


    iffi

    Wednesday, September 7, 2016 10:32 AM

Answers

  • Hi iffi.

    List<string> orgUnits = new List<string>();
    
    DirectoryEntry startingPoint = new DirectoryEntry("LDAP://DC=YourCompany,DC=com");
    
    DirectorySearcher searcher = new DirectorySearcher(startingPoint);
    searcher.Filter = "(objectCategory=organizationalUnit)";
    
    foreach (SearchResult res in searcher.FindAll()) 
    {
        orgUnits.Add(res.Path);
    }

    from

    http://stackoverflow.com/questions/5347096/how-can-i-get-a-list-of-organizational-units-from-active-directory


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    • Proposed as answer by Patrick_Liang Wednesday, September 14, 2016 9:24 AM
    • Marked as answer by Patrick_Liang Friday, September 16, 2016 10:04 AM
    Wednesday, September 7, 2016 11:57 AM
  • Hi,

    The following code snippet for your reference:

    // connect to "RootDSE" to find default naming context
    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
    
    string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();
    
    // bind to default naming context - if you *know* where you want to bind to - 
    // you can just use that information right away
    DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);
    
    // set up directory searcher based on default naming context entry
    DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
    
    // SearchScope: OneLevel = only immediate subordinates (top-level OUs); 
    // subtree = all OU's in the whole domain (can take **LONG** time!)
    ouSearcher.SearchScope = SearchScope.OneLevel;
    // ouSearcher.SearchScope = SearchScope.Subtree;
    
    // define properties to load - here I just get the "OU" attribute, the name of the OU
    ouSearcher.PropertiesToLoad.Add("ou");
    
    // define filter - only select organizational units
    ouSearcher.Filter = "(objectCategory=organizationalUnit)";
    
    // do search and iterate over results
    foreach (SearchResult deResult in ouSearcher.FindAll())
    {
        string ouName = deResult.Properties["ou"][0].ToString();
    }

    More information is here:

    http://stackoverflow.com/questions/13605618/how-to-get-list-of-ou-name-in-ad-using-domainname-using-c

    Refer to the blog below to create custom web part.

    How to Create a Custom Webpart in SharePoint 2013

    http://www.learningsharepoint.com/2012/12/24/how-to-create-a-custom-webpart-in-sharepoint-2013/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Patrick_Liang Wednesday, September 14, 2016 9:24 AM
    • Marked as answer by Patrick_Liang Friday, September 16, 2016 10:04 AM
    Thursday, September 8, 2016 6:40 AM

All replies

  • Hi iffi.

    List<string> orgUnits = new List<string>();
    
    DirectoryEntry startingPoint = new DirectoryEntry("LDAP://DC=YourCompany,DC=com");
    
    DirectorySearcher searcher = new DirectorySearcher(startingPoint);
    searcher.Filter = "(objectCategory=organizationalUnit)";
    
    foreach (SearchResult res in searcher.FindAll()) 
    {
        orgUnits.Add(res.Path);
    }

    from

    http://stackoverflow.com/questions/5347096/how-can-i-get-a-list-of-organizational-units-from-active-directory


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    • Proposed as answer by Patrick_Liang Wednesday, September 14, 2016 9:24 AM
    • Marked as answer by Patrick_Liang Friday, September 16, 2016 10:04 AM
    Wednesday, September 7, 2016 11:57 AM
  • Hi,

    The following code snippet for your reference:

    // connect to "RootDSE" to find default naming context
    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
    
    string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();
    
    // bind to default naming context - if you *know* where you want to bind to - 
    // you can just use that information right away
    DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);
    
    // set up directory searcher based on default naming context entry
    DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
    
    // SearchScope: OneLevel = only immediate subordinates (top-level OUs); 
    // subtree = all OU's in the whole domain (can take **LONG** time!)
    ouSearcher.SearchScope = SearchScope.OneLevel;
    // ouSearcher.SearchScope = SearchScope.Subtree;
    
    // define properties to load - here I just get the "OU" attribute, the name of the OU
    ouSearcher.PropertiesToLoad.Add("ou");
    
    // define filter - only select organizational units
    ouSearcher.Filter = "(objectCategory=organizationalUnit)";
    
    // do search and iterate over results
    foreach (SearchResult deResult in ouSearcher.FindAll())
    {
        string ouName = deResult.Properties["ou"][0].ToString();
    }

    More information is here:

    http://stackoverflow.com/questions/13605618/how-to-get-list-of-ou-name-in-ad-using-domainname-using-c

    Refer to the blog below to create custom web part.

    How to Create a Custom Webpart in SharePoint 2013

    http://www.learningsharepoint.com/2012/12/24/how-to-create-a-custom-webpart-in-sharepoint-2013/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Patrick_Liang Wednesday, September 14, 2016 9:24 AM
    • Marked as answer by Patrick_Liang Friday, September 16, 2016 10:04 AM
    Thursday, September 8, 2016 6:40 AM
  • Hi,

    I am getting following error

    An operations error occurred.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.

    error on line

    foreach (SearchResult deResult in ouSearcher.FindAll())
    {
        string ouName = deResult.Properties["ou"][0].ToString();
    }

    pls guide what could be an issue,

    secondly i want to show top level plus sub level OU, OU with complete hierarchy and present as a treeview. pls advise.


    iffi

    Monday, October 10, 2016 11:49 AM