I need to create a valid jqTree JSON structure from Active Directory OUs. I'm testing with a recursive method (InfoNode) for this, but I can not get it.
The resulting json goes into the string json. The first node to process is the DirectoryEntry parent with the default domain root. The recursive method (InfoNode) gets the current child node, filter by "OU" and creates the JSON properties "label" and "path". Before checks whether this node has more children to write the end of the current JSON item. Finally, if there are more children, run the method (InfoNode) again:
public static DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
public string dom = (string)root.Properties["defaultNamingContext"].Value;
public string json = "";
public Form1()
{
InitializeComponent();
DirectoryEntry parent = new DirectoryEntry("LDAP://" + dom);
InfoNode(parent);
System.IO.File.WriteAllText(@"json.txt", json);
}
void InfoNode(DirectoryEntry node)
{
string[] arr = node.Name.Split('=');
if (arr[0] == "OU")
{
json += "'children':[{'label':'" + arr[1] + "','path':'" + node.Path + "'";
if (nodo.Children == null)
{
json += "}]";
}
else
{
json += ",";
}
}
if (node.Children != null)
{
foreach (DirectoryEntry child in node.Children)
{
InfoNode(child);
}
}
}