How to: Specify the Properties Returned in a Keyword Query when using the Search Web Service
Posted
Monday, September 29, 2008 12:58 PM
by
CoreyRoth
In the past, I have showed you how to use the KeywordQuery class as well as how to use the Enterprise Search Web Service. Today, I wanted to add one small detail on how to use the web service to return your own custom managed properties. Specifying your own properties is as simple as making use of the Properties element in the XML you send to the web service. However, there are a couple of things you have to know. First, here is what the syntax looks like.
<Properties>
<Property name="DocumentType" />
<Property name="ProductId" />
</Properties>
In the example above, I want search to return my custom DocumentType and ProductId managed properties. However, if I specify just this, you will get the following error message.
Your query is missing required properties. Please make sure to select the following properties: Title, Path, Description, Write, Rank, and Size.
If you remember from my KeywordQuery post, this is because when you specify any property it removes all default properties from the list. This is easy to fix though, just add the properties it asked for. Here is what the complete document looks like.
<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">
<Query domain="QDomain">
<SupportedFormats>
<Format>urn:Microsoft.Search.Response.Document.Document</Format>
</SupportedFormats>
<Range>
<Count>50</Count>
</Range>
<Context>
<QueryText language="en-US" type="STRING">DocumentType:"Legal" Scope:"My Document Scope"</QueryText>
</Context>
<Properties>
<Property name="Title" />
<Property name="Path" />
<Property name="Description" />
<Property name="Write" />
<Property name="Rank" />
<Property name="Size" />
<Property name="DocumentType" />
<Property name="ProductId" />
</Properties>
</Query>
</QueryPacket>
One other thing to note. If you call the web service and use the QueryEx method (the one that returns a DataSet), each custom property will be returned as a single row string[] array. So if you were calling QueryEx and hoping to bind it straight to a GridView, you will be sadly disappointed as your custom properties will not be displayed. I have no idea why Microsoft implemented it this way, but that is the way it is. The weird thing is that the default properties you specify will be returned as regular strings. So if you are hoping to databind, calling Query and making use of LINQ to XML may be a better option.