Thursday, 13 March 2008

Using dynamic web reference in sharepoint Search Web service

Microsoft Office SharePoint Server 2007 provides two ways to enable search in your application

1. Query object model
2. Query web service

You can learn about them from here

What is the need of Dynamic Web reference ?

In one of my project, we are using SharePoint and we have enabled search in our application. We are using SharePoint Search web service for Search functionality. While development we add the web reference of the Search Service from our own machine( In my case MOSS 2007 is installed on my machine) i.e. http:///_vti_bin/search.asmx and search proxy class will use this information only. So the search will work on my machine but it fails on Production machine/server since it couldn’t find this used machine name ie <> . So to avoid this situation we can have something called dynamic Web reference and we can pass this information (http:///vti_bin/search.asmx) dynamically to our proxy class . Following are few very simple steps –

1.After adding the Web reference of the Search.asmx in your application, change the “URL behavior” property to “Dynamic”.

2.Create the instance of the QueryService class.

3.Change the URL property of the QueryService class to the your actual Service location.

Code Snippet:

public DataSet GetSearchResult()
string qXMLString = "<QueryPacket xmlns='urn:Microsoft.Search.Query'><Query><SupportedFormats><Format revision='1'>urn:Microsoft.Search.Response.Document:Document</Format></SupportedFormats><Context><QueryText language='en-us' type='MSSQLFT'>SELECT Title,Path,SiteName,ITEMID FROM Scope() WHERE FREETEXT(DEFAULTPROPERTIES,'SearchText') ORDER BY Rank </QueryText></Context><Range><StartAt>1</StartAt><Count>5000</Count></Range></Query></QueryPacket>"// SearchService is the Web Reference Name
SearchService.QueryService Search = new SearchService.QueryService();
Search.Url = "http://ServerName:portName/_vti_bin/search.asmx";
// You can also pick above search reference from your web config file in
// order to make it flexible.
DataSet SearchResult = Search.QueryEx(qXMLString);
return SearchResult;


No comments:

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...