Last post Sep 25, 2009 11:44 PM by jlane
Sep 04, 2009 12:57 PMemail@example.com|LINK
I simply need to swap values for AD attributes in bulk! Sounds simple? But I cant find any info to do this!
I need to swap the AD user attributes for telephoneNumber and
Thank for you help in advance.
Sep 25, 2009 11:37 PM|jlane|LINK
You could try this. It's pretty rough and I don't have a way to test it right now. I would recommend modifying it a bit to run on 1 or 2 objects before letting it loose on your whole AD store :)
Hopefully someone else knows of a better way, but this might give you some ideas.
public void SwapPhoneNumbers()
// This is the root AD path to search under
DirectoryEntry root = new DirectoryEntry("LDAP://OU=Users,DC=aaa,DC=bbb,DC=ccc", "user", "pass");
// Create DirectorySearcher and set filter
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.Filter = "(&(objectClass=user)(sAMAccountName=*))";
// Get all results
SearchResultCollection resultCollection = searcher.FindAll();
// Go through each result
foreach (SearchResult result in resultCollection)
DirectoryEntry entry = root.Children.Find("CN=" + result.GetDirectoryEntry().Name);
// Temporarily store values in this variables
string telephoneNumber = "";
string OtherTelephone = "";
// Retrieve values from properties if they exist
telephoneNumber = (string)entry.Properties["telephoneNumber"].Value;
OtherTelephone = (string)entry.Properties["OtherTelephone"].Value;
// Swap the values on the DirectoryEntry
entry.Properties["telephoneNumber"].Value = OtherTelephone;
entry.Properties["OtherTelephone"].Value = telephoneNumber;
// Save changes and dispose of entry
// cleanup everything else
AD C# LDAP
Sep 25, 2009 11:44 PM|jlane|LINK
Oh and you might not need to use root.Children.Find("CN=" + result.GetDirectoryEntry().Name) since it does *another* search, but I have found that certain property changes will not commit to the store unless you open a reference to them via the parent container.