Using SharePoint Web Services to submit a simple search request
Refer to blogs.msdn.com/dwinter/archive/2005/02/15/373076.aspx for setup if you are not familiar with creating a SharePoint OM application.
For this entry, you may want to enable the invoke via IE capability discussed in blogs.msdn.com/dwinter/archive/2005/02/21/377695.aspx
We'll start with a C# Windows Form application. I generally go ahead and add the Microsoft.SharePoint reference because I'm doing more then just a simple operation like this.
Once you have your other references set up, right click on references and select Web Reference. You'll be presented with a screen like this:
If you know the ASMX you wish to access, just type it in. Otherwise you may want to click on Web services on the local machine.
You'll be presented with a list such as this:
You can browse the various available web services and view their descriptions and available operations.
If you enabled the invoke capability, you can even test some of the operations.
For this example, I will be utilizing the search.asmx.
There are two critical operations available to us: Query and QueryEx. Query is used when you want a string result. QueryEx is more advanced and allows you to return an actual DataSet. Unless I get some questions, I won't be going into the more specific differences. We will be using Query in this example.
Once you add the reference (I called mine SearchWebService), you will see it listed in Solution Explorer like so:
I add a button and two textboxes. In buttonclick we need to initialize the web service... here is how I prefer to do this:
Note how intellisense picks up the WebReference as I defined it. Here's the actual line:
SearchWebService.QueryService myQueryService = new WSSWebRefExample.SearchWebService.QueryService();
Below is the finished function. Notice that I am setting credentials to the default credentials. If you don't do this, you will get an access denied.
With this function you can submit a simple search with a QueryRequest packet and get a QueryResponse packet with the results. You would need to parse the XML and use it of course...
private
void button1_Click(object sender, System.EventArgs e)
{
SearchWebService.QueryService myQueryService = new WSSWebRefExample.SearchWebService.QueryService();
myQueryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
if ((textBox1.Text != "") && (textBox1.Text != null))
{
try
{
myQueryService.Query(textBox1.Text);
}
catch (Exception ex)
{
textBox2.Text = ex.Message;
}
}
else
{
textBox2.Text = "Invalid Query";
}
}
Here's a sample of the text I threw in textBox1 to allow for a query to run. You need to construct your own querypacket for textBox1.
<?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">krichie</QueryText>
</Context>
</Query>
</QueryPacket>
Here's the response:
<?xml version="1.0" encoding="utf-16"?>
<ResponsePacket xmlns="urn:Microsoft.Search.Response">
<Response domain="QDomain">
<Copyright>Microsoft (c) Office SharePoint (tm) Portal Server 2003</Copyright>
<Range>
<StartAt>1</StartAt>
<Count>10</Count>
<TotalAvailable>1</TotalAvailable>
<Results>
<Document type="People" relevance="1000" xmlns="urn:Microsoft.Search.Response.Document">
<Title>Keith Richie</Title>
<Action>
<LinkUrl fileExt="aspx" size="0">dwinter/MySite/Public.aspx?guid=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</LinkUrl>
</Action>
<Description>
</Description>
<Date>2005-02-11T15:44:41</Date>
</Document>
</Results>
</Range>
<Status>SUCCESS</Status>
</Response>
</ResponsePacket>
(Yes I did change his GUID to X's)
Comments
- Anonymous
February 25, 2010
Awesome post. Thanks for sharing!