Creating Search Queries Programmatically by Using the Search Web Service in SharePoint Server 2007
Creating Search Queries Programmatically by Using the Search Web Service in SharePoint Server 2007
Creating Search Queries Programmatically by Using the Search Web Service in SharePoint Server 2007

Summary: Learn how to programmatically create search queries by using the Search Web service in Microsoft Office SharePoint Server 2007.

Office Visual How To

Applies to: Microsoft Office SharePoint Server 2007

Patrick Tisseghem, U2U

July 2007

Overview

Microsoft Office SharePoint Server 2007 includes a Web service named search.asmx that allows a remote client to execute a search query.

Code It

Developers working in Microsoft Visual Studio 2005 add a Web reference to the project that points to the search.asmx Web service, located in the _vti_bin directory off the top-level (root) of the SharePoint site. After the proxy class is created, you can start programming. Two Web methods that are exposed by the Web service—Query and QueryEx—are addressed in this Office Visual How-To.

Request Query Packet

The call to either the Query or QueryEx Web method is simple. One parameter is a blob of XML containing all of the information required for the search service to execute the query. The request query packet used for a keyword query follows.

<?xml version='1.0' encoding='utf-8' ?>
<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>
  <Query domain='QDomain'>
   <SupportedFormats>

    <Format>urn:Microsoft.Search.Response.Document.Document</Format>
   </SupportedFormats>
   <Context>

     <QueryText language='en-US' type='STRING'>query_text_placeholder</QueryText>
   </Context>
  </Query>
</QueryPacket>

Executing a Query

You execute the query by first creating an instance of the proxy class. Next, you have to perform the authentication. You can pass the credentials of the currently logged-on user (as shown in the code example), or you can create an instance of the NetworkCredential class. For the latter option, you must pass the credential information to the constructor. The request query packet containing the keyword query is passed simply as the only parameter with both the Query and QueryEx methods. The Query method returns the search results in raw XML format. The QueryEx method returns the search results as a serialized DataSet object. The proxy class makes the DataSet object directly available so that you can bind it to one of your user interface (UI) controls.

Dim searchService = New SearchServiceLab.SearchService.QueryService()
searchService.Credentials = _
     System.Net.CredentialCache.DefaultCredentials
Dim queryString as String = keywordQueryTemplate.Replace _
     ("query_text_placeholder", KeywordTextBox.Text.Trim())
Dim queryResults As String =  searchService.Query(queryString) 
MessageBox.Show(queryResults)
Dim resultDataset As DataSet =  searchService.QueryEx(queryString) 
ResultDataGridView.DataSource = resultDataset.Tables(0)
Read It

The Search Web Service exposed by Microsoft Office SharePoint Server 2007 allows for remote execution of a search query. The query must be encapsulated in a request packet and can either be formulated with the keyword syntax or the full-text SQL syntax.

Two methods allow for the execution of the query:

  • Query: Accepts a request packet and returns a response packet that includes the search results in XML format.

  • QueryEx: Accepts a request packet and returns a response packet that includes the search results formatted as a DataSet object serialized as XML.

See It Creating Search Queries using Web Service video

Watch the Video

Length: 04:36 | Size: 2.63 MB | Type: WMV file

Explore It
Community Content

searchcomponent
Added by:Noelle Mallory - MSFT

Dear All,

we have created the custom search webpart for the share point but we are unable to search the content on the specified columns in the lists and library also we are unable to search on the dates of the document.

please suggest if any one have the idea about the above porblem

Thanks in anvance

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Solution
Added by:gautam19

hey Rohit,

on similar lines we have created a console application, just copy the code and paste it into the webpart page.

one more thing..

we hv created a managed property named CreatedOn that has values from the custom defined column.

share point on its own does not gives u a column named Created On so you have to create it by your own.

here goes the code snippet

using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;
using System.Xml;
using System.Data;
using System.Runtime.InteropServices;

namespace BusinessLayerTestCases
{
public class Program
{
static void Main(string[] args)
{


//The string containing the keyword to use in the search
//string sqlString = "SELECT Tech,Title FROM Scope() WHERE Tech = 'windows'";
string sqlString = "SELECT CreatedOn, Title FROM Scope() WHERE CreatedOn >= GETGMTDATE()";
using (SPSite site = new SPSite("http://moss:42007"))
{
FullTextSqlQuery query = new FullTextSqlQuery(site);
query.QueryText = sqlString;
query.ResultTypes = ResultType.RelevantResults;
ResultTableCollection coll = query.Execute();
if ((int)ResultType.RelevantResults != 0)
{
ResultTable tblResult = coll[ResultType.RelevantResults];
if (tblResult.TotalRows == 0)
{
Console.WriteLine("No Search Results Returned.");
}
else
{
Console.WriteLine("Search Results Returned From Object Model");
DataTable dtResults = new DataTable("tblResults");
DataSet dsResults = new DataSet("dsResults");
dsResults.Tables.Add(dtResults);
dsResults.Load(coll[ResultType.RelevantResults], LoadOption.OverwriteChanges, dtResults);
Console.WriteLine("Row count:" + dtResults.Rows.Count);
foreach (DataRow dr in dtResults.Rows)
{

Console.WriteLine("\n\n");
Console.WriteLine(dr.ItemArray[1]);


}

}

}

}

Console.ReadKey();
}
}
}

you may also refer http://msdn2.microsoft.com/en-us/library/ms559713.aspx for date related functions

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View