Geeks With Blogs

Mike H. - Another Geek In Need... WebLog

Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices

 

I have been asked if I could provide an example of reading / writing the property proxyAddresses – so hopefully this will help.

 

Some basic assumptions: A binding has already been created (DirectoryEntry object), and we are going to work with that entry (we’ll refer to userEntry for our bound object reference); the proxyAddresses default prefixes (SMTP, smtp, and smtp-pager) already exist in the proxyAddresses property; the following code resides in an existing project or solution! J

 

 

public const string MailPrimaryPrefix = "SMTP";

public const string MailSecondaryPrefix = "smtp";

public const string MailPagerPrefix = "smtp-pager";

 

internal static string[] emailTypes ={ "Primary","Secondary","Pager" };

 

internal static string[] emailPrefixes =  {MailPrimaryPrefix, MailSecondaryPrefix, MailPagerPrefix };

 

public string [] emailAddresses;

 

private void GetEmailAddresses()

{

System.DirectoryServices.PropertyCollection properties = userEntry.Properties;

 

PropertyValueCollection proxyAddresses = userEntry.Properties[“proxyAddresses”];

 

for (Int32 i=0; i<emailTypes.Length; i++)

{

string emailType = emailTypes[i];

string emailPredix = emailPrefixes[i];

string foundAddress = string.Empty;

 

if (proxyAddresses!=null)

{

foreach (string address in proxyAddresses)

{

if (address.StartsWith(emailPrefix+”:”))

{

foundAddress = address.Remove(0,emailPrefix.Length+1);

break;

}

}

}

emailAddresses[i] = foundAddress;

}

} // End of GetEmailAddresses call…

 

 

// Before calling this method, make sure the string

// collection emailAddresses[] is populated with the actual

// email addresses.

private void UpdateProxyAddresses()

{

// If / when this method detects the address as the primary

// email address, it also updates the [“mail”] property…

System.DirectoryServices.PropertyCollection properties = userEntry.Properties;

 

PropertyValueCollection proxyAddresses = userEntry.Properties[“proxyAddresses”];

 

if (proxyAddresses!=null)

{

for (Int32 i=0; i<emailAddresses.Length; i++)

{

string emailType = emailTypes[i];

string emailAddress = emailAddresses[i];

int schemaIndex = Array.IndexOf(emailTypes,emailType);

 

if (schemaIndex>-1)

{

// Is it the primary address?

if (schemaIndex==0)

userEntry.Properties[“mail”].Value = emailAddress.ToString();

 

string emailPrefix = emailPrefixes[schemaIndex];

 

bool found=false;

for (int j=0; j<proxyAddresses.Count; j++)

{

string proxyAddress = proxyAddresses[j].ToString();

if (proxyAddress.StartsWith(emailPrefix+”:”))

{

proxyAddresses[j] = emailPrefix + “:” + emailAddress;

found = true;

 

}

}

if (!found)

proxyAddresses.Add(emailPrefix + “:” + emailAddress);

}

 

// Update AD…

userEntry.Properties[“proxyAddresses”] = proxyAddresses;

}

}

}

 

 

And, as always… Please forgive any typos…

 

Posted on Sunday, December 18, 2005 3:48 PM .Net Development | Back to top


Comments on this post: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi Mike,

very nice syntax coloring. How did you write it into your blog? I always have problems when posting code snippets from VS to Word and then into an html editor and from there into the blog tool.

Yours,
Alois Kraus
Left by Alois Kraus on Dec 19, 2005 7:33 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Thanks very much for this post. It demystifies a number of things about manipulating the proxyaddress attribute. I just wanted to point out a possible typo in the code, however, as I found a change to your code to be necessary in order to get it working in my envirnoment. Here it is:

I had to change...

// Update AD…

userEntry.Properties[“proxyAddresses”] = proxyAddresses;

to...

// Update AD…

userEntry.Properties[“proxyAddresses”].Value = proxyAddresses.Value;

Left by Mark Young on Apr 30, 2006 6:25 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Very good catch Mark, and thanks for pointing that out. When I'm throwing out code off the top of my head it's too easy to forget the nuances between languages.

Thanks again...
Left by MikeH on Apr 30, 2006 6:55 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi Mike, it's me again. I wonder if you might answer a couple of questions?

I've been adapting some of your code here as well as the concepts in order to do a fairly simple task: change the contents of the proxyAdresses AD attribute to reflect a change in a user's primary email address when their name changes. So if I change my last name to Smith, my email address will change from myoung@example.com to msmith@example.com. Of course this means that my primary proxyAddress needs to change from SMTP:myoung@example.com to SMTP:msmith@example.com. Now we also use a secondary proxyAddress, and this needs to change as well.

What I'm finding is that I cannot find a way to gracefully do this programmatically. The code you've posted is tremendously helpful, but it will not change the primary proxyAddress. It will replace any secondary, and using a variation on your code, I can add as many secondaries as I want, and replace them at will.

When I try to replace the primary proxyAddress, however, it simply doesn't work. With your code, I simply get the original primary address, the new secondary proxy, and the new pager address. If I try a variation that should just replace the primary, the primary is not replaced, as if I had done nothing.

One variation that partially works is the following:

public static void MakePrimaryProxyAddress (DirectoryEntry userEntry, string newproxyaddress)
{
System.DirectoryServices.PropertyCollection properties = userEntry.Properties;
PropertyValueCollection proxyAddresses = userEntry.Properties["proxyAddresses"];
if (proxyAddresses != null)
{
bool found = false;
for (int j = 0; j < proxyAddresses.Count; j++)
{
string proxyadd = proxyAddresses[j].ToString();
if (proxyadd.StartsWith("SMTP:"))
{
found = true;
Console.WriteLine("Found existing primary: " + proxyadd + "at index " + j.ToString() +", setting to secondary...");

string[] proxyparts = proxyadd.Split(':');

proxyAddresses[j] = "smtp:" + proxyparts[1];

}
}
if (found)
{
Console.WriteLine("Adding new primary proxy: " + newproxyaddress);
proxyAddresses.Insert(0, newproxyaddress);
for (int j = 0; j < proxyAddresses.Count; j++)
{
Console.WriteLine(proxyAddresses[j].ToString());
}
// Update AD…
userEntry.Properties["proxyAddresses"].Value = proxyAddresses.Value;
}
}
}

This code sets the original primary proxyAddress to be a secondary, by replacing the "SMTP:" prefix to "smtp:", then adds the new primary proxy. After this subroutine runs, I can see in ADCU that the primary address is now the new address, and the original primary is now a secondary:

Before:

SMTP:originalprimary@example.com

After:

SMTP:newprimary@example.com
smtp:originalprimary@example.com

Great. But, if I look in the mail attribute, it is still loaded with the original primary address. I can remedy the situation manually through the ACDU mmc one of two ways:

1) clear the mail attribute, this results in the mail attribute automatically updating to the new primary address

2) delete the original primary address (now changed to a secondary), this results in the same thing

This led me to believe that I could get to where I wanted to go by deleting the original primary address in a separate step. So I tried that. That resulted in my new primary proxy being deleted.

I know this a lot to process. I guess I'm asking: what am I missing? It looks as if when dealing with the primary proxyAddress, there are additional constraints that need to be met. So far from what I've seen, one is that you cannot replace the primary directly. What are the others?

If you can shed any light on this, I would be very grateful. If not, don't worry - I wouldn't even be this far along without your posted code!
Left by Mark Young on Jun 26, 2006 7:43 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hey Mark... Real quick... Check your variable usage in the nested if's... Are you using 'j' in each loop? Also, double check the code you copy/pasted to me - it shows the j< - but does not show j<0;

Let me know before I dig in deeper.
Left by MikeH on Jun 26, 2006 8:54 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
I am using j in each for loop, but the for loops are not nested. I apologize, what I posted is hard to read. I did change the looping variable in one of the for loops to k. This did not change the result.

I'm not sure what you mean by your second question. I've got the for loop definitions:

for (int j=0; j<proxyAddresses.Count; j++)
Left by Mark Young on Jun 26, 2006 10:13 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi Mark, sorry it took me so long to respond again - it's been a long week :(

If I understand what I've reviewed, you 'are' successfully updated the proxyAddresses property, but not the mail property.

From my experience, I do not remember the 'mail' property automatically reflecting the SMTP: primary from the proxyAddresses - I have not reviewed this recently, but I remember having to update the 'mail' property via code.

If you can change the SMTP: successfully, and you are checking for that in your code, when you find the SMTP: and realize it's the primary, simply branch into an if loop and update the 'mail' property at that time - that is how I believe I did it.

Please let me know.
Left by MikeH on Jul 01, 2006 11:13 AM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi Mike,

You're right about the mail attribute not automatically updating when changes are made directly to AD. When you change things with the ADCU GUI, the GUI is enforcing some constraints via proxy-sync, which I believe is provided by the inproxy.dll, the proxy generation DLL for SMTP addresses. So if you go in and clear the mail attribute, the GUI will call something that will refill it with the primary proxyaddress. I found that here: http://msexchangeteam.com/archive/2005/01/10/350132.aspx

Yes, I've tried explicitly setting the mail attribute. One example of my efforts is shown below. This is a modification of the if (found) clause from the snippet i posted above. I just added the line

userEntry.Properties["mail"].Value = addressParts[1];


which should set the mail attribute as you suggest. Unfortunately, it doesn't. I end up with the same situation as if I never tried to set the mail attribute.



if (found)
{
Console.WriteLine("Adding new primary proxy: " + newproxyaddress);
proxyAddresses.Insert(0, newproxyaddress);
string [] addressParts = newproxyaddress.Split(':');
userEntry.Properties["mail"].Value = addressParts[1];
for (int k = 0; k < proxyAddresses.Count; k++)
{
Console.WriteLine(proxyAddresses[k].ToString());
}
// Update AD…
userEntry.Properties["proxyAddresses"].Value = proxyAddresses.Value;
}

One other observation, for what it's worth. you'll notice that I chose to put the new primary proxy in with an insert call, placing the new primary in at index 0. After the changes are committed, however, the new primary is not at index 0 (I can see this by looking at the entry with adsiedit, or by reading back the propertyValueCollection in a program.) - the old primary proxy address is at index 0 (it's always the first proxyaddress). I don't know what this means, but it seems significant.

Left by Mark Young on Jul 05, 2006 8:56 PM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi Mark... I have been away on vacation for 10 days - and I apologize for not responding sooner.

I have not had a problem setting the primary "mail" property via code. In fact, I cannot reproduce what you are describing and I'm perplexed here. I am curious what is in the addressParts[1] when you attempt to set the property in your code?
Left by MikeH on Jul 10, 2006 9:44 AM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
get me on to bebo
Left by laura on Nov 12, 2007 3:45 AM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
i need porxy
Left by sain on Mar 05, 2009 6:04 AM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
Hi, do you know maybe what is the right format for proxyAddress attribute? Does it have to be sip:zarko@zarko.com or smtp:zarko@zarko.com? If I just put zarko@zarko.com I can't create Exchange mailbox.
Left by Zarko Acimovic on Aug 11, 2011 8:07 AM

# re: Update proxyAddresses property in Active Directory (AD) using System.DirectoryServices
Requesting Gravatar...
i need proxy addresses
Left by joe on May 04, 2012 9:44 AM

Your comment:
 (will show your gravatar)


Copyright © Michael J. Hamilton, Sr. | Powered by: GeeksWithBlogs.net