Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,092
    Thanks
    2
    Thanked 23 Times in 23 Posts

    Add and remove contacts from distribution lists (C#)

    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:
    Code:
    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:
    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;
                }
            }
           }
    Last edited by vinyl-junkie; 08-05-2011 at 09:23 PM.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #2
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,092
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Finally got this working! Here is my working code:
    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;
                }
            }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •