4

So I have a program that needs to add a new organizational unit under another OU. The format has to be as it is in the code below. Problem is I keep getting the same exception if I put spaces in in the name. I am able to manually add OUs with spaces. What am I a doing wrong here?

Here is the code:

        string ou = "OU=" + "New Company 99999";

        try
        {
            if (DirectoryEntry.Exists("LDAP://" + ou + ",OU=MainOrganizationalUnit,DC=domain,DC=com"))
            {
                MessageBox.Show(ou + " exists.");
            }
            else
            {
                MessageBox.Show(ou + " does not exist. Creating...");

                using (DirectoryEntry entry = new DirectoryEntry("LDAP://OU=MainOrganizationalUnit,DC=domain,DC=com"))
                {
                    using (DirectorySearcher searcher = new DirectorySearcher(entry))
                    {
                        searcher.Filter = "(" + ou + ")";
                        searcher.SearchScope = SearchScope.Subtree;
                        SearchResult result = searcher.FindOne();

                        if (result == null)
                        {
                            /* OU Creation */
                            DirectoryEntry de = entry.Children.Add(ou, "organizationalUnit");
                            de.Properties["description"].Value = "This is a Test";
                            de.CommitChanges();
                        }
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            LogWriter.Exception(Ex);
        }

When I run this code I get the following error logged: System.DirectoryServices.DirectoryServicesCOMException (0x80072037): There is a naming violation.

at System.DirectoryServices.DirectoryEntry.CommitChanges() at MyProgram.MyStaticClass.function()

David Bentley
  • 824
  • 1
  • 8
  • 27
  • If [SO:C# PrincipalContext Error “Server names cannot contain a space character”](http://stackoverflow.com/questions/28406731/c-sharp-principalcontext-error-server-names-cannot-contain-a-space-character) doesn't help, you may be able to get away with something alin to `string ou = "OU=" + @"""New Company 99999""";` – Mad Myche May 02 '17 at 00:01
  • @MadMyche I am actually using a separate work around. It seems I can rename an existing OU to something with spaces in it. `DirectoryEntries des = entry.Children;` `DirectoryEntry badObject = des.Find(ou);` `badObject.Rename("OU=With Spaces 99999");` `entry.CommitChanges();` – David Bentley May 02 '17 at 13:31

2 Answers2

1

Renaming an OU doesnt seem to be the ideal solution. Just try escaping the Spaces with a backSlash

string ou = "OU=" + "New\\ Company\\ 99999"; 

This article shows the characters that has to be escaped when using LDAP with AD.

Nasri Yatim
  • 308
  • 3
  • 13
0

So, I am currently using a work around but I feel I should not have to do this. Essentially, I am now creating a temporary OU name without spaces, then I rename it.

DirectoryEntries des = entry.Children;
DirectoryEntry badObject = des.Find(ou);
badObject.Rename("OU=With Spaces 99999");
entry.CommitChanges();
Nasri Yatim
  • 308
  • 3
  • 13
David Bentley
  • 824
  • 1
  • 8
  • 27