Perform Keyword search in Contents of SP2010 site

Jan 9, 2012 at 8:41 AM

Hi,

I need to write a HTML file which will search a Keyword (might contain more than 1 word) in the contents/Documents of SP2010 site.

I am searching hardcoded word - Links in the below code. I don't know how to give the path of the ASMX file so that the search should be performed in the mentioned SharePoint site.

Kindly look into the code and suggest the possible solution.

 

Here is the code of HTML file -

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://googlemapsharepoint.googlecode.com/files/jquery.SPServices-0.5.8.js"></script>
    </head>

    <body>
        <div>
            <script type="text/javascript">

                var queryText = "<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>"
                queryText += "<Query>"
                queryText += "<Context>"
                queryText += "<QueryText language='en-US' type='STRING'>"
                queryText += "Links"; 
                queryText += "</QueryText>"
                queryText += "</Context>"
                queryText += "</Query>"
                queryText += "</QueryPacket>";

                $(document).ready(function() {

                    $('#SearchSPContents').click(function() {

                        alert("testing..." + queryText);
                        var title, url = ""; 

                           $().SPServices({ 
                               operation: "Query", 
                               queryXml: queryText, 
                               completefunc: function(xData, Status) {

                                    if (Status != "success") {
                                        displayNewsErrorMessage();
                                        return;
                                    }

                                    $(xData.responseXML).find("QueryResult").each(function() { 
                                        var queryResult = $(xData.responseXML).find("QueryResult").text();
                                        alert('queryResult : ' + queryResult);
                                       //let's see what the response looks like 
                                       $("#result").text($(this).text()); 
                                   }); 
                               } 
                           });
                    });
                });

            </script>

            <ul id="data1"></ul>
            <input type="button" id="SearchSPContents" value="SearchSPContents"/>
            <div id="response" />
        </div>
    </body>

</html>

Coordinator
Jan 9, 2012 at 2:21 PM

kgosian:

Oddly, I don't see anything in the Query operation that allows you to specify the Search Scope. I would expect that to be a part of the schema.

If you're only looking in one site, how about doing a GetListItems for each of the lists/libraries?

M.

Jan 10, 2012 at 8:32 AM
sympmarc wrote:

kgosian:

Oddly, I don't see anything in the Query operation that allows you to specify the Search Scope. I would expect that to be a part of the schema.

If you're only looking in one site, how about doing a GetListItems for each of the lists/libraries?

M.

@sympmarc
Many thanks for your response. In the meantime I worked on alternate approach. Now I am providing the path to the ASMX file and I believe it is hitting the mentioned Sharepoint Site. However the Response is still not Valid. Kindly suggest the fix.

Here is the code -

<html>
 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
 </head>

 <body>
  <div>
   <script type="text/javascript">

    // *** Customizable parameters ***
    var quickSearchConfig = {
     scope: "All Sites",     // search scope to use
     numberOfResults: 15,    // number of results to show
    };
    
    $(document).ready(function () {

     $('#GetList').click(function() {
     
     alert('sds ' + $("#quickSearchTextBox").val());

      var queryXML =
       " \
        \
        <Format>urn:Microsoft.Search.Response.Document.Document</Format> \
         \
         SCOPE:\"" + quickSearchConfig.scope + "\"" + $("#quickSearchTextBox").val() + " \
         \
        \
        1" + quickSearchConfig.numberOfResults + " \
        false \
        <TrimDuplicates>true</TrimDuplicates> \
        true \
        true \
        true \
        true \
        true \
       ";
      
      var soapEnv =
       " \
          \
         \
          " + escapeHTML(queryXML) + " \
         \
          \
       ";
      
      // Call web service
      $.ajax({
       url: "http://path/to/WSDL/file.asmx",
       type: "POST",
       dataType: "xml",
       data: soapEnv,
       complete: processResult,
       contentType: "text/xml; charset=\"utf-8\""
      });

     });
    });

       function escapeHTML (str) {
       return str.replace(/&/g,'&').replace(/g,'>');
    }

    
    function processResult(xData, status) {
    alert(xData);
    //alert('$(this) : ' + $(this));
    //alert('xData.responseXML : ' + xData.responseXML);
    
    //alert('$(this).text() : ' + $(this).text());
    alert('In processResult ' + xData.responseXML.xml);
    
     //let's see what the response looks like 
     //$("#response").text($(this).text());
     //$('#response').text(xData.responseXML.xml);
    }

   </script>

   <ul id="data1"></ul>
   <input style="width: 100px" id="quickSearchTextBox" class="ms-sbplain" title="Enter search words"
        style="width: 170px" alt="Enter search words" maxlength="200" value="" />
   <input type="button" id="GetList" value="GetList"/>

   <div id="response" />

  </div>
 </body>

</html>

Coordinator
Jan 10, 2012 at 2:30 PM

Since you're no longer using SPservices, you're on your own. It doesn't look to me like your query is valid, though.

M.

Jan 18, 2012 at 10:02 AM

I solved this by giving the older version of jquery.min.js

 

<html>
 <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
 </head>

 <body>
  <div>
   <script type="text/javascript">

    // *** Customizable parameters ***
    var quickSearchConfig = {
     scope: "People", //"All Sites",     // search scope to use
     numberOfResults: 15    // number of results to show
    };
    
    $(document).ready(function () {

     $('#GetList').click(function() {
     
     //alert('sds ' + $("#quickSearchTextBox").val());

      var queryXML =
       "<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' >SCOPE:\"" + quickSearchConfig.scope + "\"" + $("#quickSearchTextBox").val() + "</QueryText> \
        </Context> \
       <SortByProperties><SortByProperty name='Rank' direction='Descending' order='1'/></SortByProperties> \
        <Range><StartAt>1</StartAt><Count>" + quickSearchConfig.numberOfResults + "</Count></Range> \
        <EnableStemming>false</EnableStemming> \
        <TrimDuplicates>true</TrimDuplicates> \
        <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery> \
        <ImplicitAndBehavior>true</ImplicitAndBehavior> \
        <IncludeRelevanceResults>true</IncludeRelevanceResults> \
        <IncludeSpecialTermResults>true</IncludeSpecialTermResults> \
        <IncludeHighConfidenceResults>true</IncludeHighConfidenceResults> \
       </Query></QueryPacket>";
      
      var soapEnv =
       "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
         <soap:Body> \
        <Query xmlns='urn:Microsoft.Search'> \
          <queryXml>" + escapeHTML(queryXML) + "</queryXml> \
        </Query> \
         </soap:Body> \
       </soap:Envelope>";
      
      // Call web service
      $.ajax({
       url: "http://<SHAREPOINT-SITE>/_vti_bin/search.asmx",
       type: "POST",
       dataType: "xml",
       data: soapEnv,
       complete: processResult,
       contentType: "text/xml; charset=\"utf-8\""
      });

     });
    });

       function escapeHTML (str) {
       return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
    }

    
    function processResult(xData, status) {
     //alert(xData);
     //alert('xData.responseXML : ' + xData.responseXML);
     //alert('In processResult ' + xData.responseXML.xml);
     
     //alert('$(this) : ' + $(this));
     //alert('$(this).text() : ' + $(this).text());
    
     //let's see what the response looks like 
     //$("#response").text($(this).text());
     $('#response').text(xData.responseXML.xml);
    }

   </script>

   <ul id="data1"></ul>
   <input style="width: 100px" id="quickSearchTextBox" class="ms-sbplain" title="Enter search words"
        style="width: 170px" alt="Enter search words" maxlength="200" value="" />
   <input type="button" id="GetList" value="GetList"/>

   <div id="response" />

  </div>
 </body>

</html>

Coordinator
Jan 18, 2012 at 2:55 PM

That's surprising, but I'm glad you got it running.

M.