I'm trying to do the same thing in C# and can't figure out what I'm doing wrong.
The following is code where I successfully added a Contact to Active Directory:
public void CreateContact(UsersDTO userDTO, ContactsDTO contactsDTO, UserContactsDTO userContactsDTO)
{
// Binding object.
DirectoryEntry directoryEntry;
try
{
directoryEntry = new DirectoryEntry("LDAP://OU=Contacts,OU=CLSRS,DC=dev,DC=lcb", username, password);
directoryEntry.RefreshCache();
DirectoryEntry contact = directoryEntry.Children.Add("CN=" + userDTO.FirstName + " " + userDTO.LastName, "Contact");
contact.Properties["sn"].Add("CN=" + userDTO.FirstName + " " + userDTO.LastName);
contact.Properties["givenName"].Add(userDTO.FirstName);
contact.Properties["mail"].Add(contactsDTO.EmailAddress);
contact.CommitChanges();
}
catch (Exception e)
{
// Do some error processing
var msg = e.Message.ToString();
}
}
Now I want to add that Contact to a distribution list, but I'm getting an error - There is a naming violation - and I don't know how to fix it. Here is my code:
vinyl-junkie
Member
12 Points
2 Posts
Add and remove contacts from distribution lists (C#)
Aug 05, 2011 06:34 PM|LINK
I searched the forums and found a thread similar to what I'm trying to do here:
http://forums.asp.net/t/1292982.aspx/1?add+and+remove+users+from+a+given+distribution+list+VB
I'm trying to do the same thing in C# and can't figure out what I'm doing wrong.
The following is code where I successfully added a Contact to Active Directory:
public void CreateContact(UsersDTO userDTO, ContactsDTO contactsDTO, UserContactsDTO userContactsDTO) { // Binding object. DirectoryEntry directoryEntry; try { directoryEntry = new DirectoryEntry("LDAP://OU=Contacts,OU=CLSRS,DC=dev,DC=lcb", username, password); directoryEntry.RefreshCache(); DirectoryEntry contact = directoryEntry.Children.Add("CN=" + userDTO.FirstName + " " + userDTO.LastName, "Contact"); contact.Properties["sn"].Add("CN=" + userDTO.FirstName + " " + userDTO.LastName); contact.Properties["givenName"].Add(userDTO.FirstName); contact.Properties["mail"].Add(contactsDTO.EmailAddress); contact.CommitChanges(); } catch (Exception e) { // Do some error processing var msg = e.Message.ToString(); } } Now I want to add that Contact to a distribution list, but I'm getting an error - There is a naming violation - and I don't know how to fix it. Here is my code:public void AddContactToGroup(string FirstName, string LastName, string GroupName) { // Binding object. DirectoryEntry directoryEntry; try { directoryEntry = new DirectoryEntry("LDAP://CN=" + GroupName + ",OU=Groups,OU=CLSRS,DC=dev,DC=lcb", username, password); directoryEntry.RefreshCache(); DirectoryEntry groupContact = directoryEntry.Children.Add("CN=" + FirstName + " " + LastName, "Contact"); groupContact.Properties["member"].Add("CN=" + FirstName + " " + LastName); groupContact.CommitChanges(); } catch (Exception e) { // Do some error processing var msg = e.Message.ToString(); throw; } } }vinyl-junkie
Member
12 Points
2 Posts
Re: Add and remove contacts from distribution lists (C#)
Aug 05, 2011 08:20 PM|LINK
Finally got this working! Here is my working code:
public void AddContactToGroup(string FirstName, string LastName, string GroupName) { // Binding object. DirectoryEntry directoryEntry; try { directoryEntry = new DirectoryEntry("LDAP://CN=" + GroupName + ",OU=Groups,OU=CLSRS,DC=dev,DC=lcb", username, password); directoryEntry.RefreshCache(); DirectoryEntry groupContact = new DirectoryEntry("LDAP://CN=" + FirstName + " " + LastName + ",OU=Contacts,OU=CLSRS,DC=dev,DC=lcb", username, password); directoryEntry.Properties["member"].Add(groupContact.Properties["distinguishedName"].Value); directoryEntry.CommitChanges(); } catch (Exception e) { // Do some error processing var msg = e.Message.ToString(); throw; } }