Mar
22
2011

How to: Query Search using the Web Service

This is copied from here

The first place to start is by adding a web reference to your project.  You can query SharePoint Search or Enterprise Search in the exact same manner it is just a matter of which web service you make a reference to.  SharePoint search can be queried at a URL similar to the one below.

http://moss-server/_vti_bin/spsearch.asmx

To query Enterprise Search, the URL will look similar to this.  Both web services have the same methods.  They just query different indexes.

http://moss-server/_vti_bin/search.asmx

To execute a query, you have to build an XML document. I posted about the XML you post to the service in the past, now I will give a complete example and then also explain some of the options you can configure when querying.   The type of query is specified on the QueryText element using the Type attribute.  A value of MSSQLFT like in the XML below is used to do a full text query.

 

<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="MSSQLFT">SELECT Title, Path, Description, Write, Rank, Size FROM Scope() WHERE CONTAINS('Accounting') AND "Scope" = 'Corporate Documents'</QueryText>
    </Context>
  </Query>
</QueryPacket>

In the example above, I have a simple query that is looking for documents with the word Accounting in the Corporate Documents scope.  Issuing a keyword query is similar but it uses a Type of STRING.  In this example, we are searching on the managed property city with a value of Austin.

 

<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">City:"Austin"</QueryText>
    </Context>
  </Query>
</QueryPacket>

There are two methods used to query search.  The Query method returns the results as XML.  The QueryEx method returns the results as an ADO.NET dataset.  They both take the same input XML document like the ones above.  The code is pretty simple. Here I create a new reference to the web service (I named it QueryWebService) and I pass it credentials.  Like any SharePoint web service, you have to pass it credentials.  You can specify custom ones or just use the credentials of the application you are running using DefaultCredentials.

 

QueryWebService.QueryService queryService = new QueryWebService.QueryService();
queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

For this example, I am just going to build the input XML using a StringBuilder. 

 

queryXml.Append("<QueryPacket xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000\">");
queryXml.Append("<Query domain=\"QDomain\">");
queryXml.Append("<SupportedFormats>");
queryXml.Append("<Format>");
queryXml.Append("urn:Microsoft.Search.Response.Document.Document");
queryXml.Append("</Format>");
queryXml.Append("</SupportedFormats>");
queryXml.Append("<Range>");
queryXml.Append("<Count>50</Count>");
queryXml.Append("</Range>");
queryXml.Append("<Context>");
queryXml.Append("<QueryText language=\"en-US\" type=\"STRING\">");
queryXml.Append("City:\"Austin\"");
queryXml.Append("</QueryText>");
queryXml.Append("</Context>");
queryXml.Append("</Query>");
queryXml.Append("</QueryPacket>");

This query is similar to the one from above, but I added a Range and Count element.  By default, you will only get 10 results back, by changing this element I will get back 50 in this case.  You can also specify a StartAt element to start on a specific row.  This would be useful if you are building paging into something.  Then I just execute the query like so.

 

string resultXml = queryService.Query(queryXml.ToString());

 

This returns an XML document that looks similar to the following.

 

<ResponsePacket xmlns="urn:Microsoft.Search.Response">
  <Response domain="QDomain">
    <Range>
      <StartAt>1</StartAt>
      <Count>10</Count>
      <TotalAvailable>7312</TotalAvailable>
      <Results>
        <Document relevance="1000" xmlns="urn:Microsoft.Search.Response.Document">
          <Title>Document 1</Title>
          <Action>
            <LinkUrl size="0">http://moss-server/Documents/Document1.docx</LinkUrl>
          </Action>
          <Description />
          <Date>2008-07-15T19:59:43.2511787-05:00</Date>
        </Document>
        <Document relevance="1000" xmlns="urn:Microsoft.Search.Response.Document">
          <Title>Document 2</Title>
          <Action>
            <LinkUrl size="0">http://moss-server/Documents/Document2.docx</LinkUrl>
          </Action>
          <Description />
          <Date>2008-07-15T19:59:43.2511787-05:00</Date>
        </Document>
      </Results>
    </Range>
    <Status>SUCCESS</Status>
  </Response>
</ResponsePacket>

From here, you have something that you can easily work with using LINQ to XML.  If you are feeling lazy, you can always just use QueryEx to get a dataset as well.  You can configure quite a few options in how things are searched using the input XML document.  The schema is in the SDK if you are interested.  Hopefully, this will help the next time you need to query search.

Comments (20) -

Michael Wylie

Exciting blog post! I really enjoyed reading this content. You are a brilliant writer. I like your way of presentation. Keep it up.

<a href="http://www.themikewylie.com">Michael Wylie</a>

iphone app developer

Hello I wish to to share a comment here concerning you to definitely be able to inform you just how much i personally Loved this particular study. I have to elope in order to aTurkey Day time Supper but desired to leave ya an easy comment. We preserved you Same goes with be returning subsequent function to read more of yer quality articles. Keep up the quality work.

Cablewave

I'm in complete agreement with a lot of the information in the following paragraphs.  You certainly are a special article writer have real profit put your own views directly into apparent phrases.  Anyone will be able to understand why.

antennasystems.com/.../cable-connectors-rf.html

mobile app developer

Hello I wish to to share a comment here concerning you to definitely be able to inform you just how much i personally Loved this particular study. I have to elope in order to aTurkey Day time Supper but desired to leave ya an easy comment. We preserved you Same goes with be returning subsequent function to read more of yer quality articles. Keep up the quality work.

Cablewave

I'm in complete agreement with a lot of the information in the following paragraphs.  You certainly are a special article writer have real profit put your own views directly into apparent phrases.  Anyone will be able to understand why.

d&#233;pannage plomberie

Hey there I am so glad I found your site, I really found you by accident, while I was researching on Aol for something else, Anyhow I am here now and would just like to say thanks for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic work.

depannage-general.be/...ie-bruxelles-belgique.html

collection agency

Another very strong and powerful post. I’ve been reading through some of your previous posts and finally decided to drop a comment on this one. I signed up for your newsletter, so please keep up the informative posts!http://www.crgcollections.com/

collection agency

Another very strong and powerful post. I’ve been reading through some of your previous posts and finally decided to drop a comment on this one. I signed up for your newsletter, so please keep up the informative posts!

http://www.crgcollections.com/

how to make apps

Hey there I am so glad I found your site, I really found you by accident, while I was researching on Aol for something else, Anyhow I am here now and would just like to say thanks for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic work.

thrivemobilemarketing.com/website-mobilization

how to create an app

I'm in complete agreement with a lot of the information in the following paragraphs.  You certainly are a special article writer have real profit put your own views directly into apparent phrases.  Anyone will be able to understand why.

healthly recipes


Great stuff here. The information and the detail were just perfect. I think that your perspective is deep, its just well thought out and really fantastic to see someone who knows how to put these thoughts down so well. Great job on this.

<a href="http://paleoaholic.com/">healthly recipes</a>

beautiful african american women

Hey there I am so glad I found your site, I really found you by accident, while I was researching on Aol for something else, Anyhow I am here now and would just like to say thanks for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic work.

http://beautywired.com

uhf antenna antennas

Hi there! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

AVA5

Hi there! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

plombier d&#233;bouchage

Please tell me that youre heading to keep this up! Its so great and so important. I cant wait to read a lot more from you. I just feel like you know so substantially and know how to make people listen to what you might have to say. This blog is just too cool to become missed. Terrific stuff, genuinely. Please, PLEASE keep it up!

surrounding

This is one technology that I would love to be able to use for myself. It’s definitely a cut above the rest and I can’t wait until my provider has it. Your insight was what I needed. Thanks

http://www.crgcollections.com/

surrounding

This is one technology that I would love to be able to use for myself. It’s definitely a cut above the rest and I can’t wait until my provider has it. Your insight was what I needed. Thanks

electroplating plastic

Please tell me that youre heading to keep this up! Its so great and so important. I cant wait to read a lot more from you. I just feel like you know so substantially and know how to make people listen to what you might have to say. This blog is just too cool to become missed. Terrific stuff, genuinely. Please, PLEASE keep it up!

http://www.alexandriaplating.com

rwculverlaw

Please tell me that youre heading to keep this up! Its so great and so important. I cant wait to read a lot more from you. I just feel like you know so substantially and know how to make people listen to what you might have to say. This blog is just too cool to become missed. Terrific stuff, genuinely. Please, PLEASE keep it up!

http://www.rwculverlaw.com/

cristina model24

Hello everybody  

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading