Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Gridview Edit Row 10 44
ASP.net produce a pdf from a GridView 2 33
Async and await with MVC and Web Api 3 30
401 - Error 6 25
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

14 Experts available now in Live!

Get 1:1 Help Now