views:

76

answers:

1

Hi,

I am looking for a bit of guidence on how to create mail contacts in AD. This is a follow on question from SO Q#1861336.

What I am trying to do is add a load of contact objects into an OU in Active Directory. I've been using the examples on CodeProject, however they only show how to make new user etc.

How do I create a contact using c#? Is it similar to creating a new user but with different LDAP type attributes?

My plan is to then run the enable-mailcontact cmdlet powershell script to enable Exchange 2010 to see the contact in the GAL.

As you can see by my questions I don't usually deal with c# or Active Directory so any help/pointers would be really useful before I start playing with this loaded gun.

Thanks,

Grant

A: 

it is similar to creating user

just us "contact" instead of "user" as an object

here is a dirty code (not tested)

public string CreateContact(string ldapPath, string userName, 
    string userEmail)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "contact");
        newUser.Properties["DisplayName"].Value = userName;

        //important attributs are
        newUser.Properties["targetAddress"].Value = "SMTP:" + userEmail;
        newUser.Properties["mailNickname"].Value = userName;

        // I'm still trying to figureout what shoud I use here!
        newUser.Properties["showInAddressBook"].Value = ???;

        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

hope it helps you

Data-Base