While working with Active Directory within SharePoint we probably don’t need to specify the domain or the root container. We can the current values. Here is a simple method from a console application just to demonstrate:
internal static void GetDomain() { var context = new DirectoryContext(DirectoryContextType.Domain); var domain = Domain.GetDomain(context); Console.WriteLine("Full domain:"); Console.WriteLine(domain.Name); //takana.local Console.WriteLine(); Console.WriteLine("root container"); var parts = domain.Name.Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries); var dcParts = parts.Select(n => "dc=" + n).ToArray(); var d = string.Join(",", dcParts); //dc=takana, dc=local Console.WriteLine(d); }
First we get get the full domain, then we split and join them again.
