Specify a Search Scope When Using search.asmx Service

This article will outline how to setup your code:  link

For my app, I needed to leverage a search scope as part of my query.  Note: when setting up the XML packet, pay close attention to your spacing and closing tags.  If not, you will run into endless error’s that have little to no relevant description.

Example:

<ResponsePacket xmlns="urn:Microsoft.Search.Response"><Response domain="QDomain"><Status>ERROR_BAD_REQUEST</Status><DebugErrorMessage>Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 442.</DebugErrorMessage></Response></ResponsePacket>

Here is my code example. Keep in mind that I’ve already setup my web reference (_vti_bin/Search.asmx).

                var webServ = "http://rootSiteUrl/mysitecolleciton/_vti_bin/Search.asmx";

                QueryService qService = new QueryService();
                qService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                qService.Url = webServ;

                SearchQuery sQuery = new SearchQuery();
                var querySetup = sQuery.SearchString("my search value");
                var queryResults = qService.QueryEx(querySetup);

    class SearchQuery
    {
        public string SearchString (string entityId )
        {
            StringBuilder queryXml = new StringBuilder();
            queryXml.Append("<QueryPacket xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000\">");
            queryXml.Append("<Query domain=\"QDomain\">");
            queryXml.Append("<SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format>");
            queryXml.Append("</SupportedFormats>");
            queryXml.Append("<Context>");
            queryXml.Append("<QueryText language='en-US' type='MSSQLFT'>");
            queryXml.Append("SELECT Title, Path, Description, Write, Rank, Size FROM Scope() WHERE \"Scope\" = 'Big - Scope' AND CONTAINS(owsOrderx0020Number,'" + entityId +"')");
            queryXml.Append("</QueryText>");
            queryXml.Append("</Context>");
            queryXml.Append("</Query>");
            queryXml.Append("</QueryPacket>");

            return queryXml.ToString();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *