Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert html to string 6 38
Modal Popup Extender control 1 38
Testing of the web services 1 33
How to use NFS (Network File System) in Asp.net mvc 5? 4 43
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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