{"id":162,"date":"2015-11-12T12:55:35","date_gmt":"2015-11-12T12:55:35","guid":{"rendered":"http:\/\/www.softreeconsulting.com\/?p=162"},"modified":"2015-11-12T12:55:35","modified_gmt":"2015-11-12T12:55:35","slug":"retrieve-all-users-and-groups-from-active-directory-ad","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/","title":{"rendered":"Retrieve users and groups from Active Directory (AD)"},"content":{"rendered":"<p>Below code describes how to get all users and all groups from active directory (AD Server) Using System.DirectoryServices<\/p>\n<p>Steps:-<\/p>\n<ol>\n<li>Add System.Directoryservices.dll and System.Directoryservices.AccountManagement.dll which provides access to active directory.<\/li>\n<li>Using System.DirectoryServices.AccountManagement<\/li>\n<li>I have used a generic list (collection of string) to store account name of user and group.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #44c7c5;\">Program<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #0000ff;\">void<\/span> Main(<span style=\"color: #0000ff;\">string<\/span>[] args)<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">\u00a0<span style=\"color: #44c7c5;\">List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt; _userOrGroupColl = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt;();<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">\u00a0<span style=\"color: #44c7c5;\">PrincipalContext<\/span> pContext = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">PrincipalContext<\/span> (<span style=\"color: #44c7c5;\">ContextType<\/span>.Domain, YOUR_DOMAIN);<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #008000;\">\/\/For User<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">\u00a0<span style=\"color: #44c7c5;\">UserPrincipal<\/span> userPrincipal = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">UserPrincipal<\/span> (pContext);<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\"><span style=\"color: #44c7c5;\">PrincipalSearcher<\/span> userSearch = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">PrincipalSearcher<\/span> (userPrincipal);<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #008000;\">\/\/For Group<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\"><span style=\"color: #44c7c5;\">GroupPrincipal<\/span> grpPrincipal = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">GroupPrincipal<\/span> (pContext);<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">\u00a0<span style=\"color: #44c7c5;\">PrincipalSearcher<\/span> grpSearch = <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #44c7c5;\">PrincipalSearcher<\/span> (grpPrincipal);<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">foreach<\/span> (<span style=\"color: #44c7c5;\">UserPrincipal<\/span> result <span style=\"color: #0000ff;\">in<\/span> userSearch.FindAll())<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">if<\/span> (result != <span style=\"color: #0000ff;\">null<\/span>)<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 120px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">if<\/span> (result.SamAccountName!= <span style=\"color: #0000ff;\">null<\/span>)<\/span><\/p>\n<p style=\"padding-left: 150px;\"><span style=\"color: #000000;\">\u00a0_userOrGroupColl.Add(result.SamAccountName);<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">foreach<\/span> (<span style=\"color: #44c7c5;\">GroupPrincipal<\/span> result <span style=\"color: #0000ff;\">in<\/span> grpSearch.FindAll())<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\"><span style=\"color: #0000ff;\">if<\/span> (result != <span style=\"color: #0000ff;\">null<\/span>)<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\">{<\/span><\/p>\n<p style=\"padding-left: 120px;\"><span style=\"color: #000000;\">\u00a0<span style=\"color: #0000ff;\">if<\/span> (result.SamAccountName != <span style=\"color: #0000ff;\">null<\/span>)<\/span><\/p>\n<p style=\"padding-left: 150px;\"><span style=\"color: #000000;\">_userOrGroupColl.Add(result.SamAccountName);<\/span><\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p><span style=\"color: #000000;\">}<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below code describes how to get all users and all groups from active directory (AD Server) Using System.DirectoryServices Steps:- Add System.Directoryservices.dll and System.Directoryservices.AccountManagement.dll which provides access to active directory. Using System.DirectoryServices.AccountManagement I have used a generic list (collection of string) to store account name of user and group. &nbsp; class Program { static void Main(string[] args) { \u00a0List&lt;string&gt; _userOrGroupColl = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[39,40,41,42],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-sharepoint","tag-how-to-get-all-users-and-groups-from-active-directory","tag-how-to-get-users-and-groups-from-active-directory","tag-how-to-get-users-from-active-directory","tag-retrieve-all-users-and-groups-from-active-directory"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Retrieve users and groups from Active Directory (AD) - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Retrieve users and groups from Active Directory (AD) - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"Below code describes how to get all users and all groups from active directory (AD Server) Using System.DirectoryServices Steps:- Add System.Directoryservices.dll and System.Directoryservices.AccountManagement.dll which provides access to active directory. Using System.DirectoryServices.AccountManagement I have used a generic list (collection of string) to store account name of user and group. &nbsp; class Program { static void Main(string[] args) { \u00a0List&lt;string&gt; _userOrGroupColl = [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-12T12:55:35+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Retrieve users and groups from Active Directory (AD) - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/","og_locale":"en_US","og_type":"article","og_title":"Retrieve users and groups from Active Directory (AD) - Softree Technology","og_description":"Below code describes how to get all users and all groups from active directory (AD Server) Using System.DirectoryServices Steps:- Add System.Directoryservices.dll and System.Directoryservices.AccountManagement.dll which provides access to active directory. Using System.DirectoryServices.AccountManagement I have used a generic list (collection of string) to store account name of user and group. &nbsp; class Program { static void Main(string[] args) { \u00a0List&lt;string&gt; _userOrGroupColl = [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/","og_site_name":"Softree Technology","article_published_time":"2015-11-12T12:55:35+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Retrieve users and groups from Active Directory (AD)","datePublished":"2015-11-12T12:55:35+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/"},"wordCount":149,"commentCount":1,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"keywords":["how to get all users and groups from Active Directory","how to get users and groups from Active Directory","how to get users from Active Directory","Retrieve all users and groups from Active Directory"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/","name":"Retrieve users and groups from Active Directory (AD) - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"datePublished":"2015-11-12T12:55:35+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retrieve-all-users-and-groups-from-active-directory-ad\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Retrieve users and groups from Active Directory (AD)"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}