Solved

Limiting the number of results returned in an Active Directory query using PrincipalSearcher

Posted on 2013-01-03
2
1,179 Views
Last Modified: 2013-01-07
I have a username textbox on my page. I've created an autocomplete feature which queries Active Directory for all users where the username contains the text in the textbox. Because this is an autocomplete feature, I want to limit the number of results returned to optimize performance. The only methods I see available are FindOne() and FindAll(). Is there a way to get the first 'n' records matching the criteria of the query?

My code currently breaks out of the loop after reading 'n' records but the FindAll() method still returns all matching records causing a performance hit.

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string[] GetUsernames(string partialUsername, int maxResults, string serverName)
        {
            List<string> userNames = new List<string>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, serverName);
                
                UserPrincipal userPrincipal = new UserPrincipal(principalContext);
                userPrincipal.Name = "*" + partialUsername + "*";
                PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);
                                
                PrincipalSearchResult<Principal> results = principalSearcher.FindAll();
                foreach (var result in results)
                {
                    if (!string.IsNullOrEmpty(result.Name))
                    {

                            userNames.Add(result.Name);
                      
                            if (userNames.Count >= maxResults)
                                    break;
                    }
                }
            });
            userNames.Sort();
            return userNames.ToArray();
        }

Open in new window

0
Comment
Question by:ZaDrizzle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 38743483
Hi,

you might try this:
Add the following code at your line 13:
((DirectorySearcher)principalSearcher.GetUnderlyingSearcher()).SizeLimit = 10;

Open in new window


http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit.aspx

HTH
Rainer

BTW: If you would have added SharePoint topic, I would have seen your question earlier ;-)
0
 

Author Closing Comment

by:ZaDrizzle
ID: 38751689
Thanks Rainer!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question