Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

People Search using search.asmx

4.50/5 (2 votes)
12 Jun 2007 1  
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