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;
}