Replacing proxyAddresses values

4.1k views Asked by At

We have a c# script that needs to update/replace the proxyAddresses contents. I believe I understand how to add a range of values as follows:

DirectoryEntry entry = new DirectoryEntry(myConnectString);
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(sAMAccountName=" + theUser + ")";
SearchResult result = Dsearch.FindOne();
if (result != null)
{
 if (result.Properties.Contains("proxyAddresses"))
 {
   DirectoryEntry Uentry = result.GetDirectoryEntry();
   Uentry.Properties[proxyAddresses].AddRange(new object[] {"[email protected]", "[email protected]"});
   Uentry.CommitChanges();
 }
}

However- feel free to correct any errors in the above code. If this looks correct - my understanding is that AddRange will append my new values rather than replacing the current values. Can someone please describe how I can remove/replace the existing contents of proxyAddresses with these new values..? Thanks in advance.

1

There are 1 answers

4
Mauricio Gracia Gutierrez On BEST ANSWER

This will replace the proxyAddresses properties

Uentry.Properties["proxyAddresses"].value = new object[] {"[email protected]", "[email protected]"};

There are more examples about how to work with proxyAddresses here