views:

24

answers:

1

Hi.

I'm trying to add new object to existing organisational unit in Active Directory. Following code is used to do this.

It runs without errors. But new object is not created after this. Please advise what I'm doing wrong here.

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace TestAdObjectCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry root = new DirectoryEntry("LDAP://servername/OU=Services,OU=PCX,DC=q2q,DC=xenyq,DC=com", "Administrator", "pass");
            DirectoryEntry newItem = root.Children.Add("test_node", "valid_schema_class_name");
            root.CommitChanges();
            root.Close();
            root.Dispose();
        }
    }
}
A: 

What kind of object are you trying to create??

First of all, all LDAP object always have a prefix, DC= for domain component, OU= for organizational unit, CN= for common name.

Also, many LDAP objects have minimal requirements for what they need to be considered valid; e.g. a user or a group must have a unique samAccountName.

So again - what are you trying to create??

If you wrap your creation code into a try..catch - do you get any exceptions? If so - what are they??

static void Main(string[] args)
{
    DirectoryEntry root = new DirectoryEntry("LDAP://servername/OU=Services,OU=PCX,DC=q2q,DC=xenyq,DC=com", "Administrator", "pass");

    try
    {
        DirectoryEntry newItem = root.Children.Add("CN=test_node", "valid_schema_class_name");

        root.CommitChanges();
    }
    catch(Exception exc)
    {
       string error = exc.GetType().FullName + ": " + exc.Message;
    } 
}
marc_s
Marc, I tried modifying the code as you advised. There is no exception at all. But the object is not created either. Actually I'm trying to create an object of custom class defined in AD schema. I'd appreciate any other ideas on this problem. :)
Humanier
Marc_s, thanks for the hint with "CN" prefix.
Humanier