Thursday 9 January 2014

Utilizing SPServices to Call SharePoint Search Web Service


SPServices from CodePlex provides a much simpler way to interact with SharePoint Search Web Service, as you can see from an example below. Please note that I've only requested documents from the query, if you want to return everything, remove ISDOCUMENT=true from the query.

 

<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<script language="javascript" src="jquery-1.8.2.min.js"></script>
<script language="javascript" src="jquery.SPServices-0.7.2.min.js"></script>
<script language="javascript">
    (function () {
        function querySearchService(scopeName, keywordToSearch, maxItemCount) {
            keywordToSearch = keywordToSearch.trim().replace(/\s+/g, "+");
            var queryText = "<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>"
            queryText += "<Query>"
            queryText += "<Range><StartAt>1</StartAt><Count>" + maxItemCount + "</Count></Range>"
            queryText += "<Context>"
            queryText += "<QueryText language='en-US' type='MSSQLFT'>"
            queryText += "SELECT Title, Path, IsDocument, ContentClass, Author, Rank, Size, Description, Write FROM scope() WHERE CONTAINS ('" + keywordToSearch + "') AND \"scope\"='" + scopeName + "' AND ISDOCUMENT=true ORDER BY Rank DESC"
            queryText += "</QueryText>"
// Keyword syntax
//   queryText += "<QueryText language='en-US' type='String'>" + keywordToSearch;
//   queryText += " Scope:" + scopeName;
//   queryText += "</QueryText>";

            queryText += "</Context>"
            queryText += "</Query>"
            queryText += "<EnableStemming>True</EnableStemming>"
            queryText += "<TrimDuplicates>True</TrimDuplicates>"
            queryText += "</QueryPacket>";
 
            alert(queryText);
 
            $().SPServices({
                operation: "Query",
                queryXml: queryText,
                completefunc: function (xData, Status) {
                    $(xData.responseXML).find("QueryResult").each(function () {
                        var array = new Array();
                        alert($(this).text());
                        var result = $.parseXML($(this).text());//$("<XmlResult>" + $(this).text() + "</XmlResult>");
                        var matches = result.find("Document");
                        if (matches.length == 0) {
                            alert("Nothing Found!");
                            return;
                        }
                        $("#dynamic-search-results").empty();
                        matches.each(function () {
                            url = $("Properties>Property:nth-child(2)>Value", $(this)).text();                        //$("Action>LinkUrl", $(this)).text();  
                            title = $("Properties>Property:nth-child(1)>Value", $(this)).text();
                            $("#resultDiv").append($("<p></p>").append($("<a></a>").prop("href", url).text(title)));
                        });
 
                    });
                }
 
            });
 
        }
 
        $(document).ready(function () {
            querySearchService("Entire Site", "SharePoint", 10);
 
        });
 
    })();
 
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 2</title>
</head>
 
<body>
 <div id="resultDiv"></div>
</body>
 
</html>
 

No comments:

Post a Comment