I have a .csv file with EmployeeNumber, TelephoneNumber, IPPhone Number listed in a text file I need a way to loop thru all the records in the text file and modify the 2 phone attributes in LDAP please help. This is totally frustrating
If you're using .NET 3.5 (or you can update to it), check out the System.DirectoryServices.AccountManagement
namespace - makes a lot of things a lot easier.
Read up on it: Managing Directory Security Principals in the .NET Framework 3.5 and check out all the properties surfaced by the new UserPrincipal
class in .NET 3.5.
The other place I'd recommend is Richard Mueller's web site - he has lots of Excel sheets which show all the AD / LDAP attributes, where on the interactive tools you'll find those, and so forth. Highly useful!
Combine those two resources, and you should be able to do whatever it is you need to do!
Update: if you cannot update to .NET 3.5 (which is really just like a service pack on top of .NET 2....), you would have to do the following steps:
- import the CSV into a
List<CSVRecord>
- I'd use the free FileHelpers library to do that; yourCSVRecord
would hold the three fields in your CSV file create a
DirectorySearcher
class based on your search root (your domain or a sub-container thereof); find the correct LDAP search filter to find your user byEmployeeId
loop over the entries in your list, and for each entry
- search the directory for that user
- if found: grab the
DirectoryEntry
from yourSearchResult
and update the two attributes - call
.CommitChanges()
on thatDirectoryEntry
I don't think there's any other way, really, to do this - there's no magic way to select all users at once, or update them all at once.
Update #2:
Here are some resources you can check out:
- Quick List of lots of C# code samples
- How to do almost anything in Active Directory using C#
- Search results for "Active Directory" on Codeproject
and the ultimate book on the subject:
Joe Kaplan / Ryan Dunn: The .NET Developer's Guide to Directory Services Programming