Dagur

Hi,

I'm trying to get a list of people from active directory and I wrote a little web part for it (basically I borrowed the AD code from http://www.c-sharpcorner.com/UploadFile/frankalonzo/GlobalAddressList11262005053740AM/GlobalAddressList.aspx ArticleID=1c1b6bf4-b468-42f6-a40b-b1f1f677dde6
)


Code Snippet

public ArrayList returngal()
{
DirectorySearcher objsearch = new DirectorySearcher();
string strrootdse = objsearch.SearchRoot.Path;
DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);
objdirentry.Username = "net\Username";
objdirentry.Password = "password";
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))";
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;
objsearch.PropertiesToLoad.Add("cn");
objsearch.PropertyNamesOnly = true;
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
objsearch.Sort.PropertyName = "cn";
SearchResultCollection colresults = objsearch.FindAll();
ArrayList arrGal = new ArrayList();
foreach (SearchResult objresult in colresults)
{
arrGal.Add(objresult.GetDirectoryEntry().Properties["cn"].Value);
}
objsearch.Dispose();
return arrGal;
}



I tried this function in a console application both on my computer and the server and it works fine. But when I run it in a web part I get this error:

An operations error occurred.


which happens when objsearch.FindAll() is called. I tried it with and without the username/password and it doesn't matter.

I'm doing this on Sharepoint 2007 (WSS3, MOSS).







Re: SharePoint - Development and Programming DirectorySearcher on Sharepoint: An operations error occurred

Dagur

Maybe I should point out that i put the web part in the GAC, if that matters.




Re: SharePoint - Development and Programming DirectorySearcher on Sharepoint: An operations error occurred

Dagur

Can anyone tell me if this is even possible :-/




Re: SharePoint - Development and Programming DirectorySearcher on Sharepoint: An operations error occurred

Bajanl

The solution to this would be to try using SPSecurity.RunWithElevatedPrivileges. This will allow the code to run with enough Privileges to access Active Directory. I have tested your code in a web part in MOSS 2007 and it worked fine. You can also feel free to add any exceptiona handling as you see fit.

public static void DirectorySearcher(System.Web.UI.HtmlTextWriter writer)

{

ADUsers adu = new ADUsers(); //Only use for static method ...class name ... or use regular method not static

SPSite siteColl = SPContext.Current.Site;

SPWeb site = SPControl.GetContextWeb(adu.Context);

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using(SPSite ElevatedSiteCollection = new SPSite(siteColl.ID))

{

using(SPWeb ElevatedSite = ElevatedSiteCollection.OpenWeb(site.ID))

{

DirectorySearcher objsearch = new DirectorySearcher();

string strrootdse = objsearch.SearchRoot.Path;

DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);

objsearch.Filter = "(& (mailnickname=*)(objectClass=user))";

objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;

objsearch.PropertiesToLoad.Add("cn");

objsearch.PropertyNamesOnly = true;

objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;

objsearch.Sort.PropertyName = "cn";

SearchResultCollection colresults = objsearch.FindAll();

ArrayList arrGal = new ArrayList();

foreach(SearchResult objresult in colresults)

{

arrGal.Add(objresult.GetDirectoryEntry().Properties["cn"].Value);

writer.Write("<BR>"+ objresult.GetDirectoryEntry().Properties["cn"].Value);

}

objsearch.Dispose();

}

}

});

}