views:

606

answers:

2

I've made a small application that reads out all the departments in our AD, and saves it as an xml file for each company that the departments belong to. But I also need to add all the groups that users in each department in each company is member of.

Here is a snippet of one of the xml files that my code produces. I want to add the groups as a child to each department node.

<departments>
  <department name="Administrasjon">
    <group></group>
  </department>
  <department name="Barnehage">
    <group></group>
  </department>
  <department name="Bibliotek">
    <group></group>
  </department>
  <department name="Frivilligsentralen">
    <group></group>
  </department>
</departmets>

The code for generating and saving this is shown below. Is it possible to get what i want? Any help would be appreciated.

static void Main(string[] args)
        {
            ArrayList companies = new ArrayList();
            companies = GetCompaniesFromAD();
            for (int i = 0; i < companies.Count; i++)
            {
                GetCompanyAndDepAndCreateXML( companies[i].ToString() );
            }                
        }

        public static ArrayList GetCompaniesFromAD()
        {         
            string path = "LDAP://myserver/OU=Brukere,OU=Felles,DC=bla,DC=bla,DC=bla";


            DirectoryEntry entry = new DirectoryEntry(path);
            ArrayList companies = new ArrayList();
            string sFilter = String.Format("(&(objectCategory=Person)(objectClass=user)(company=*))");
            string[] attribs = new string[] { "department", "company" };

            using (DirectorySearcher dirsearcher = new DirectorySearcher(entry,sFilter,attribs))
            {                
                foreach (SearchResult sResult in dirsearcher.FindAll())
                {
                    StringBuilder companyNames = new StringBuilder();

                    if(sResult.Properties.Contains("company"))
                    {
                        foreach (object o in sResult.Properties["company"])
                        {
                            companyNames.AppendFormat("{0}", o);
                            companies.Add(o.ToString());                            
                        }                        
                    } //end if "company                    

                } //end foreach                
            } //end using..



            ArrayList companyNoDuplicates = new ArrayList();
            companyNoDuplicates = RemoveDuplicates(companies);
            return companyNoDuplicates;
          }

        public static void GetCompanyAndDepAndCreateXML(string companyName)
        {
            string path = "LDAP://myserver/OU=Brukere,OU=Felles,DC=bla,DC=bla,DC=bla";


            DirectoryEntry entry = new DirectoryEntry(path);            
            ArrayList departments = new ArrayList();
            string sFilter = String.Format("(&(objectCategory=Person)(objectClass=user)(company=" + companyName + "))");
            string[] attribs = new string[] { "department" };

            using (DirectorySearcher dirsearcher = new DirectorySearcher(entry, sFilter, attribs))
            {
                foreach (SearchResult sResult in dirsearcher.FindAll())
                {                    
                    StringBuilder departmentNames = new StringBuilder();                    

                    if (sResult.Properties.Contains("department"))
                    {
                        foreach (object o in sResult.Properties["department"])
                        {
                            departmentNames.AppendFormat("{0}", o);
                            departments.Add(o.ToString());                            
                        }
                    } //end if department
                } //end foreach                
            } //end using..


            ArrayList departmentNoDuplicates = new ArrayList();
            departmentNoDuplicates = RemoveDuplicates(departments);                        

            generateXmlDoc(departmentNoDuplicates, companyName);
        }

        public static void generateXmlDoc(ArrayList departments, string companyName)
        {
            XElement xml = new XElement("departments");
            for (int i = 0; i < departments.Count; i++)
            {
                XElement node = new XElement("department",
                    new XAttribute("name", departments[i].ToString()),
                    new XElement("group", "")
                    );
                xml.Add(node);
            }
            xml.Save(companyName + ".xml");

        }

        public static ArrayList RemoveDuplicates(ArrayList items)
        {
            ArrayList noDuplicates = new ArrayList();
            foreach (string strItem in items)
            {
                if (!noDuplicates.Contains(strItem.Trim()))
                {
                    noDuplicates.Add(strItem.Trim());
                }
            }
            noDuplicates.Sort();

            return noDuplicates;
        }
+2  A: 

This is going to be much easier to implement if you use System.DirectoryServices.AccountManagement as it is more intuitive for accomplishing your task.

SCMcDonnell
I haven't read about that. I'm totally new working with AD. But is it possible to achieve my goal using System.DirectoryServices.AccountManagement?
Svein Erik
+2  A: 

As "SCMcDonnell" said, if you're on .NET 3.5 or higher, definitely use the S.DS.AccountManagement namespace.

Find an excellent article describing this new namespace on MSDN - Joe Kaplan and Ethan Wilansky did a great job explaining the new features and how to use them.

Marc

marc_s
Yes, I'm on .NET 3.5, I'll read the article and google a bit. Have you seen any code similiar to the one I'm after?
Svein Erik
No, not specifically - but as far as I recall, enumerating and searching is one of the areas of improvement, so maybe you can do things a bit easier with S.DS.AM in the end
marc_s