Click here to Skip to main content
Click here to Skip to main content

People Search using search.asmx

By , 12 Jun 2007
 

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 = "https://servername/_vti_bin/search.asmx";
queryPeople.PreAuthenticate = true;
queryPeople.Credentials = new System.Net.NetworkCredential("userID", "passwd", "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

About the Author

Vinay Yeluri
Web Developer
United States United States
Member
Sr. .NET Developer
MS in ComputerScience
Old Dominion University

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionException - ERROR_SERVERmemberSuhail Jamaldeen12 Oct '12 - 1:36 
SuggestionSharepoint settingsmemberMember 94068774 Sep '12 - 22:50 
Generalsearch result.membersema z16 Feb '10 - 10:02 
Generalis there any way to bring everybody in ADmembersema z17 Jun '09 - 14:18 
AnswerRe: is there any way to bring everybody in ADmembergbelzile4 Nov '09 - 9:30 
GeneralGood infomemberAnthonyKilhoffer3 Aug '07 - 4:58 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 12 Jun 2007
Article Copyright 2007 by Vinay Yeluri
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid