Reputation: 470
Error:Internal : Could not execute code stage because exception thrown by code stage: The (&(objectClass=user)(|(displayName)) search filter is invalid.
emailAddress = "";
displayName = "Smith";
DirectorySearcher oSearch = new DirectorySearcher();
//oSearch.Filter = "name=" + displayName;
//oSearch.Filter = "(&(objectClass=user)(|(sAMAccountName = displayName))";
oSearch.Filter = "(&(objectClass=user)(|(displayName))";
SearchResult oResult = oSearch.FindOne();
DirectoryEntry oEntry = oResult.GetDirectoryEntry();
emailAddress = oEntry.Properties["emailAddress"].Value.ToString();
Trying to get email address based on display name.
Tried these as well.
(&(objectClass=user)(&(displayName = {Smith}))
oSearch.Filter = string.Format("(&(objectClass=user)(&(displayName = {Smith})))", displayName);
Thanks!
Upvotes: 0
Views: 2376
Reputation: 19178
The search filter is invalid/incorrect since you're incorrectly using the OR(|) operator.
Though there is no condition given in the question, I'll assume a case that you're trying to find the email-address of the user whose display name is "Smith".
In this case, your filter would be:
oSearch.Filter = "(&(objectClass=user)(displayName=Smith))";
// check the placement of brackets and the operator carefully as shown above.
// if you need to search for names starting with "Smith", use wildcard: (displayName=Smith*)
SearchResult oResult = oSearch.FindOne();
if (null != oResult) // check for null values everywhere in your code to avoid NPE
DirectoryEntry oEntry = oResult.GetDirectoryEntry();
if (null != oEntry) // check for null values, else NP exception will be thrown
emailAddress = oEntry.Properties["emailAddress"].Value.ToString();
Please refer the TechNet article Active Directory: LDAP Syntax Filters for more information.
Upvotes: 1