Share via


Using SharePoint 2010 Search webservice in Javascript for FAST search for sharepoint 2010

Here we will see a simple example of calling SharePoint 2010 search web service in javascript. This example is for FAST search for SharePoint 2010, so we will use FQL in our query packet.

We will be using XMLHttpRequest object for our request and response.

We need to undertand the below XML structures for moving ahead:

1] SOAP request for search service. Here, the request XML is for 'QueryEX' method. 'queryXml' will contain the actual query packet for search, as indicated by string.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <QueryEx xmlns="https://microsoft.com/webservices/OfficeServer/QueryService">
      <queryXml>string</queryXml>
    </QueryEx>
  </soap:Body>
</soap:Envelope>

 Let's assume this is represented by variable 'requestXML'.

2] SOAP response for search service. Here, the response XML is for 'QueryEX' method. 'QueryExResult' will contain the result, as indicated by xml.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <QueryExResponse xmlns="https://microsoft.com/webservices/OfficeServer/QueryService">
      <QueryExResult><xsd:schema>schema</xsd:schema>xml</QueryExResult>
    </QueryExResponse>
  </soap:Body>
</soap:Envelope>

  Let's assume this is represented by variable 'responseXML'.

3] FQL Query Packet for search, indicating the search criteria, refiners, sorting, etc.

<QueryPacket Revision=\"1000\">
  <Query>
    <Context>
      <QueryText language=\"en-US\"type=\"FQL\">string(\"-\",Mode=\"AND\")</QueryText>
    </Context>
    <SupportedFormats Format=\"urn:Microsoft.Search.Response.Document.Document\"/>
    <ResultProvider>FASTSearch</ResultProvider>
    <Range>
      <StartAt>1</StartAt>
      <Count>20</Count>
    </Range>
    <EnableStemming>true</EnableStemming>
    <EnableSpellCheck>Suggest</EnableSpellCheck>
    <IncludeSpecialTermsResults>true</IncludeSpecialTermsResults>
    <IncludeRelevantResults>true</IncludeRelevantResults>
    <ImplicitAndBehavior>true</ImplicitAndBehavior>
    <TrimDuplicates>true</TrimDuplicates>
    <Properties>
      <Property name=\"Rank\"/>
      <Property name=\"Title\" />
      <Property name=\"Author\"/>
      <Property name=\"Size\" />
      <Property name=\"Path\" />
      <Property name=\"Write\"/>
      <Property name=\"SiteName\" />
      <Property name=\"HitHighlightedSummary\"/>
      <Property name=\"HitHighlightedProperties\"/>
      <Property name=\"ContentClass\"/>
      <Property name=\"IsDocument\"/>
      <Property name=\"Url\" />
      <Property name=\"FileExtension\"/>
      <Property name=\"SpSiteUrl\"/>
      <Property name=\"docvector\"/>
      <Property name=\"fcocount\" />
      <Property name=\"fcoid\"/>
    </Properties>
  </Query>
</QueryPacket>

 Let's assume this is represented by variable 'queryPacket'. this will replace the value string in step [1] for requestXML.

 

We can write a function incorporating all the above for calling the service and getting the response as follows:

function RetrieveSearchData()

{

    //get the request object

    var requestObject = new XMLHttpRequest();

    //Initializes request and specifies the method, URL, and other information for the request.

    requestObject.open("POST", "https://SERVER:PORT/_vti_bin/search.asmx", false);

    //set request header

    requestObject.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    //Get FQL queryPacket along with the SOAP envelope

    var packet = requestXML;

    //Sends an HTTP request to the server and receives a response

    requestObject.send(packet);

    //process responseXML

}

 

There are multiple ways to achieve the above processing in AJAX, ASP.net or traditional C#. This example is just one of the ways to process SharePoint webservice in javascript code.

Comments

  • Anonymous
    November 17, 2013
    Hi Can you please let me know where is the response object here? and how to handle it?

  • Anonymous
    November 17, 2013
    I am getting Bad request error on chrome