Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#
Article

People Search using search.asmx

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
12 Jun 2007 70.4K   18   6
Provides information on how to do a people search using search.asmx

Introduction

This article aims at providing the reader information on how to use the Sharepoint search webservice to do a people search and there by also attain a degree of wildcard search functionality.

Background

The reason for developing this code is to provide a customizable search interface which is completely dettached from the sharepoint UI. This was primarily done as a part of a mobile sharepoint project and this allowed us to reduce the data rate of the page.

Using the code

In this project, I have a class Search.cs which handles the building of the query and calling the web service. The class would then return the xml string with the query results to the caller who would transform it to the right format using xslt. The search.asmx which is the search web service that is exposed by sharepoint is present in the following directory

http://servername/_vti_bin/search.asmx.

Steps Involved

1. Create a web reference to the web service and name it something like PeopleSearch

2. Once you have the reference in the class use it like below:

PeopleSearch.QueryService queryPeople = new PeopleSearch.QueryService();
queryPeople.Url = "<a href="https://servername/_vti_bin/search.asmx">https://servername/_vti_bin/search.asmx</a>";
queryPeople.PreAuthenticate = true;
queryPeople.Credentials = new System.Net.NetworkCredential("userID", "<a>passwd</a>", "domain");

3. Once you have the reference you can build the query which is as shown below, in my search
I'm mainly intereseted in the first,last names or phone and hence the query as shown below
StringBuilder query = new StringBuilder();
            try
            {
                query.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                query.Append("<QueryPacket xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000\">");
                query.Append("<Query domain=\"QDomain\">");
                query.Append("<SupportedFormats>");
                query.Append("<Format>urn:Microsoft.Search.Response.Document.Document</Format>");
                query.Append("</SupportedFormats>");
                query.Append("<Context>");
                query.Append("<QueryText language=\"en-US\" type=\"MSSQLFT\">");
                query.Append("SELECT ");
                query.Append("preferredname, ");
                query.Append("Department, ");
                query.Append("WorkPhone, ");
                query.Append("WorkEmail, ");
                query.Append("Path ");
                query.Append("FROM SCOPE() ");
                query.Append("WHERE ");
                query.Append("(\"DAV:contentclass\" = 'urn:content-class:SPSPeople') ");
                if ((fname.Length > 0 || lname.Length > 0) && phone.Length == 0)
                {
                    if ((fname.Length > 0) && (lname.Length > 0))
                    {
                        query.Append(" AND (\"FirstName\" LIKE '" + fname + "%') AND (\"LastName\" LIKE '" + lname + "%')");
                    }
                    else if ((fname.Length > 0) && (lname.Length == 0))
                    {
                        query.Append(" AND (\"FirstName\" LIKE '" + fname + "%')");
                    }
                    else if ((fname.Length == 0) && (lname.Length > 0))
                    {
                        query.Append(" AND (\"LastName\" LIKE '" + lname + "%')");
                    }
                }
                else
                {
                    query.Append(" AND (\"WORKPHONE\" LIKE '%" + phone + "%')");
                }
                query.Append("</QueryText>");
                query.Append("</Context>");
                query.Append("</Query>");
                query.Append("</QueryPacket>");
4. Once you have the query built you can call the Query method of the webservice which would execute the query and return the xml.
resultString = queryPeople.Query(queryString);

Use the appropriate xslt to transform your results, this allows you to do a wild card search.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Sr. .NET Developer
MS in ComputerScience
Old Dominion University

Comments and Discussions

 
QuestionException - ERROR_SERVER Pin
Suhail Jamaldeen12-Oct-12 1:36
Suhail Jamaldeen12-Oct-12 1:36 
SuggestionSharepoint settings Pin
Member 94068774-Sep-12 22:50
Member 94068774-Sep-12 22:50 
Generalsearch result. Pin
sema z16-Feb-10 10:02
sema z16-Feb-10 10:02 
Generalis there any way to bring everybody in AD Pin
sema z17-Jun-09 14:18
sema z17-Jun-09 14:18 
AnswerRe: is there any way to bring everybody in AD Pin
gbelzile4-Nov-09 9:30
gbelzile4-Nov-09 9:30 
GeneralGood info Pin
AnthonyKilhoffer3-Aug-07 4:58
AnthonyKilhoffer3-Aug-07 4:58 
Great article. Short and to the point, and very helpful. Thank you.

Anthony S. Kilhoffer
Senior Practice Consultant
EMC Global Services, Microsoft Practice

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.