In my Infopath form,I need to populate a dropdown with the names of the domains from the Active Directory Services.
The DirectorySearcher class and the FindAll() method helps us to get the list of the domains in the ADS server where I am running this webservice.
Is it possible to obtain the list of domains from a remote ADS server.
If so can u suggest a solution.
The code which I am currently using requires me to run the webservices in a machine which is the ADS server.
The code is as follows...
public XmlNode GetListOfDomains()
{
DataSet dsDomain = new DataSet();
DataSet dsForest=new DataSet();
dsForest.ReadXml("d:\\forests.xml");
Hashtable hstDomains=new Hashtable();
try
{
//Get list of Forest entires from Application object.
Hashtable hstForest = StartRead("d:\\forests.xml");//This XML file contains the domain name.
Hashtable hstDomainPath=new Hashtable();
//string strADSPath="";
foreach(string key in hstForest.Keys)
{
try
{
//Create an instance of Directory entry for the given forest.
DirectoryEntry deADS=new DirectoryEntry(key);
//create an instance of DirectorySearcher for the directory entry
DirectorySearcher dirSrchADS=new DirectorySearcher(deADS);
//Assign LDAP filter to DirectorySearcher to find only Domain objects
dirSrchADS.Filter="(objectCategory=domainDNS)";
//Load only the "name" property of directory searcher
dirSrchADS.PropertiesToLoad.Add("name");
//loop thru the directorysearcher for all the possible findings
foreach(SearchResult result in dirSrchADS.FindAll())
{
//create an instance of DirectoryEntry from the searchresult object
DirectoryEntry deDomains = result.GetDirectoryEntry();
//hstDomains.Add(((object)deDomains.Properties["name"].Value).ToString(),((object)deDomains.Properties["name"].Value).ToString());
//There could be more than one Domains in the same name, so add all the domains with same name in a hashtable
if(!hstDomains.ContainsKey(deDomains.Properties["name"].Value.ToString()))
hstDomainPath =new Hashtable();
hstDomainPath.Add(deDomains.Path.ToString(),hstForest[key].ToString());
//Add the hstDomainPath hashtable into hstDomains hashtable.
hstDomains.Add(((object)deDomains.Properties["name"].Value).ToString(),hstDomainPath);
}
//if no domains are found nullify the hstDomains hashtable
if(hstDomains.Count==0)
hstDomains=null;
}
catch(Exception ex)
{
throw ex;
}
}
}
catch(Exception ex)
{
throw ex;
}
XmlDataDocument dom = new XmlDataDocument(dsForest );
return dom.DocumentElement;
}