Retrieving Mail and Name of a MCMS user

MCMS 2002 only offers you the login of the current user. Not the Name or Email address.

In some situations – like Email notification – it is necessary to get the Email address and other information which is stored in Active Directory.

The sample code below allows to read this information from Active Directory.

You need to adjust the values in User, ADConnectionString.

User needs to be given as user account with a domain. The ADConnectionString needs to be given in the following format: LDAP://CN=Users,DC=dompart1,DC=dompart2,…DC=dompartN. (the code below assumes the following UPN: stefang@europe.corp.company.com)

public string GetMail(string User)

{

string UserFilter = "(&(objectClass=user) (SAMAccountName=" + User + "))";

string ADConnectionString = "LDAP://CN=Users,DC=europe,DC=corp,DC=company,DC=com";

DirectoryEntry dirEntry = new DirectoryEntry(ADConnectionString);

if (dirEntry != null)

{

DirectorySearcher dirSearcher = new DirectorySearcher();

dirSearcher.SearchRoot = dirEntry;

dirSearcher.Filter = UserFilter;

dirSearcher.SearchScope = SearchScope.Subtree;

dirSearcher.PropertiesToLoad.Add("mail");

SearchResult result = dirSearcher.FindOne();

string MailAddr = result.Properties["mail"][0].ToString();

dirSearcher.Dispose();

dirEntry.Dispose();

return(MailAddr);

}

else

{

return("nobody@nowhere.com");

}

}

A good idea is to store the ADConnectionString in the web.config or another location where it can be changed without a recompile of the module.

The same method as described above can be used to retrieve the friendly name of the user. Just replace the property name mail with displayname.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.