AD Query- works in Form App but not in Sharepoint!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: patrick (patl_at_reply.newsgroup.msn.com)
Date: 08/04/04


Date: Wed, 4 Aug 2004 10:31:21 +0100

This is really weird.....

I got the following code (at the end of this message) working in a Win Forms
C# .NET application, however when done as a Web Part for Sharepoint Portal
Server/WSS 2003, it doesn't work. The forms App and the WebParts have the
same label name and text box names. In both Web.config for the SPS
Application and the machine.config, <identity impersonate="true" /> is set
without a username (so it should be passing in the identify of the person
who logged onto SPS/WSS.

When I run this via a Forms application, I get the attributes of users
matching the firstname and surname listed in the label.

However, when I run it as a Web Parts (after compiling it into a Cab File,
installed it using stsadm, populated it to a Gallery, and added it to a
subsite under the portal), I get the following displayed

Note that when I did an LDIFDE as folllows, the ADOutput.ldf does indicate
that the givenName and sn attribute were exported, so they do exists!!
    LDIFDE -f ADOutput.ldf -r "(objectClass=user)"

-----------------Start of message displayed-----------------
Results Results for filter
(&(objectClass=user)(givenName=*C*)(sn=*C*))---beforeSearch--

System.Runtime.InteropServices.COMException (0x8007200A): The specified
directory service attribute or value does not exist at
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_AdsObject() at
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindAll() at
AcmeWebParts.ActiveDirectoryWebPart.ADHandler(Object sender, EventArgs e)
-----------------End of message displayed--------------------

--------------Start of Code Snippet (ADHandler)--------------
//ADHandler is the event handler for a button which when clicked is meant to
search the AD.
   string strTemp= "";
   strTemp = "Results for filter (&(objectClass=user)(givenName=*" +
txtFirstname.Text + "*)(sn=*" + txtSurname.Text + "*))---";
   try
   {
    DirectoryEntry objADRoot = new
DirectoryEntry("LDAP://dc=myDomain,dc=co,dc=test");
    DirectorySearcher mySearcher = new DirectorySearcher(objADRoot);
    //(giveName=" + txtFirstName.Text + "*)
    mySearcher.Filter= "(&(objectClass=user)(givenName=*" +
txtFirstname.Text + "*)(sn=*" + txtSurname.Text + "*))";
    strTemp += "beforeSearch--";
    SearchResultCollection colSearchResults = mySearcher.FindAll();
    strTemp += "afterSearch--<br>";
    foreach(SearchResult objEachResult in colSearchResults)
    {
     // Iterate through each property name in each SearchResult.
     foreach(string strPropKey in objEachResult.Properties.PropertyNames)
     {
      strTemp = strTemp + strPropKey + "=";
      // Retrieve the value assigned to that property name
      // in the ResultPropertyValueCollection.
      ResultPropertyValueCollection colVal =
objEachResult.Properties[strPropKey];
      // Iterate through values for each property name in each SearchResult.
      foreach(Object objProp in colVal)
      {
       // Handle results. Be aware that the following WriteLine
       // only returns readable results for properties that are strings.
       strTemp = strTemp + objProp.ToString();
      }
      strTemp= strTemp +"<br>\n";
     }
    }
    lblDisplay.Text = strTemp;
   }
   catch (Exception excp)
   {
    lblDisplay.Text= strTemp + "<br><br>" + excp.ToString();
   }
--------------End of Code Snippet (ADHandler)--------------


Quantcast