<p>In one of my applications, I am querying active directory to get a list of all users below a given user (using the "Direct Reports" thing). So basically, given the name of the person, it is looked up in AD, then the Direct Reports are read. But then for every direct report, the tool needs to check the direct reports of the direct reports. Or, more abstract: The Tool will use a person as the root of the tree and then walk down the complete tree to get the names of all the leaves (can be several hundred)</p>

<p>Now, my concern is obviously performance, as this needs to be done quite a few times. My idea is to manually cache that (essentially just put all the names in a long string and store that somewhere and update it once a day).</p>

<p>But I just wonder if there is a more elegant way to first get the information and then cache it, possibly using something in the System.DirectoryServices Namespace?</p>

[{'answer_id': 33299, 'author': 'kemiller2002', 'author_id': 1942, 'author_profile': 'https://Stackoverflow.com/users/1942', 'pm_score': 2, 'selected': False, 'text': "<p>Active Directory is pretty efficient at storing information and the retrieval shouldn't be that much of a performance hit.  If you are really intent on storing the names, you'll probably want to store them in some sort of a tree stucture, so you can see the relationships of all the people.  Depending on how the number of people, you might as well pull all the information you need daily and then query all the requests against your cached copy.  </p>\n"}, {'answer_id': 36730, 'author': 'Phil Bennett', 'author_id': 2995, 'author_profile': 'https://Stackoverflow.com/users/2995', 'pm_score': 2, 'selected': False, 'text': "<p>AD does that sort of caching for you so don't worry about it unless performance becomes a problem.  I have software doing this sort of thing all day long running on a corporate intranet that takes thousands of hits per hour and have never had to tune performance in this area.  </p>\n"}, {'answer_id': 39091, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 3, 'selected': True, 'text': '<p>In order to take control over the properties that you want to be cached you can call \'RefreshCache()\' passing the properties that you want to hang around:</p>\n\n<pre class="lang-c# prettyprint-override"><code>System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry();               \n\n// Push the property values from AD back to cache.\n\nentry.RefreshCache(new string[] {"cn", "www" });\n</code></pre>\n'}, {'answer_id': 39099, 'author': 'Guy', 'author_id': 993, 'author_profile': 'https://Stackoverflow.com/users/993', 'pm_score': 2, 'selected': False, 'text': '<p>Depends on how up to date you want the information to be.  If you <strong>must have</strong> the very latest data in your report then querying directly from AD is reasonable.  And I agree that AD is quite robust, a typical dedicated AD server is actually very lightly utilised in normal day to day operations <em>but best to check with your IT department / support person.</em></p>\n\n<p>An alternative is to have a daily script to dump the AD data into a CSV file and/or import it into a SQL database.  (Oracle has a SELECT CONNECT BY feature that can automatically create multi-level hierarchies within a result set.  MSSQL can do a similar thing with a bit of recursion IIRC).</p>\n'}]
['https://Stackoverflow.com/questions/33250', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']