Solved

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

Posted on 2013-01-03
2
1,087 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now