...

View Full Version : Resolved Add and remove contacts from distribution lists (C#)



vinyl-junkie
08-05-2011, 08:24 PM
I have successfully added a Contact to Active Directory programatically. Now I'm trying to add that Contact to a distribution list and having problems getting it to work. The latest version of my code is giving me the error "There is a naming violation." Please help!

Here is the code where I successfully added a Contact:

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();
}
}


Here is the code for trying to add the Contact to a distribution list:

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
08-05-2011, 09:23 PM
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;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum