15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

OC138 ❘ ChaPTer 52 directOry services<br />

returns a DirectoryEntries collection that collects DirectoryEntry objects. You iterate through all<br />

DirectoryEntry objects to display the name of the child objects:<br />

using (var de = new DirectoryEntry())<br />

{<br />

de.Path = "LDAP://magellan/OU=thinktecture, DC=cninnovation, DC=local";<br />

}<br />

Console.WriteLine("Children of {0}", de.Name);<br />

foreach (DirectoryEntry obj in de.Children)<br />

{<br />

Console.WriteLine(obj.Name);<br />

}<br />

code snippet DirectoryServicesSamples/Program.cs<br />

When you run the program, the common names of the objects are displayed:<br />

Children of OU=thinktecture<br />

OU=Admin<br />

CN=Buddhike de Silva<br />

CN=Christian Nagel<br />

CN=Christian Weyer<br />

CN=Consultants<br />

CN=demos<br />

CN=Dominick Baier<br />

CN=Ingo Rammer<br />

CN=Neno Loye<br />

In this example, you see all the objects in the organizational unit: users, contacts, printers, shares,<br />

<strong>and</strong> others. If you want to display only some object types, you can use the SchemaFilter property of<br />

the DirectoryEntries class. The SchemaFilter property returns a SchemaNameCollection. With this<br />

SchemaNameCollection, you can use the Add() method to define the object types you want to see. Here,<br />

you are just interested in seeing the user objects, so user is added to this collection:<br />

using (var de = new DirectoryEntry())<br />

{<br />

de.Path = "LDAP://magellan/OU=thinktecture, DC=cninnovation, DC=local";<br />

}<br />

Console.WriteLine("Children of {0}", de.Name);<br />

de.Children.SchemaFilter.Add("user");<br />

foreach (DirectoryEntry obj in de.Children)<br />

{<br />

Console.WriteLine(obj.Name);<br />

}<br />

As a result, you see only the user objects in the organizational unit:<br />

Cache<br />

Children of OU=thinktecture<br />

CN=Christian Nagel<br />

CN=Christian Weyer<br />

CN=Dominick Baier<br />

CN=Ingo Rammer<br />

CN=Jörg Neumann<br />

CN=Richard Blewett<br />

To reduce the network transfers, ADSI uses a cache for the object properties. As mentioned earlier, the<br />

server isn’t accessed when a DirectoryEntry object is created; instead, with the first reading of a value<br />

from the directory store, all the properties are written into the cache so that a round trip to the server isn’t<br />

necessary when the next property is accessed.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!