0

I am having issue while searching using query text in a specific list for cross-domain. Also I am not able to search in a specific site, when I use the following get query, url:

appweburl + "/_api/search/query?querytext ='amar'?@target='" + hostweburl + "'",

Also I am not able to search in a specific list, when I use the following get query, url:

appweburl + "/_api/search/query?querytext=‌​'amar AND path:htt‌​ps://xyz.sharepoi‌​nt.com/Lists/emp data​'?@target='" + hostweburl + "'",

0

Grant add-in search permission and try with JSOM.

Here is my test script.

<script type="text/javascript">
        var hostweburl;
        var appweburl;

        // Load the required SharePoint libraries
        $(document).ready(function () {
            //Get the URI decoded URLs.
            hostweburl =
                decodeURIComponent(
                    getQueryStringParameter("SPHostUrl")
            );
            appweburl =
                decodeURIComponent(
                    getQueryStringParameter("SPAppWebUrl")
            );

            // resources are in URLs in the form:
            // web_url/_layouts/15/resource
            //var scriptbase = hostweburl + "/_layouts/15/";

            var clientContext = SP.ClientContext.get_current();
            //var hostWebContext = new SP.AppContextSite(clientContext, hostweburl);            

            var keywordQuery =
            new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);

            keywordQuery.set_queryText('test AND path:' + hostweburl + '/Lists/mylist2');

            var searchExecutor =
            new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);

            var results = searchExecutor.executeQuery(keywordQuery);

            clientContext.executeQueryAsync(successHandler, errorHandler);

            // Function to handle the success event.
            // Prints the data to the page.
            function successHandler() {                
                for (var i= 0; i < results.m_value.ResultTables[0].ResultRows.length; i++) {
                    alert(results.m_value.ResultTables[0].ResultRows[i].Title) 
                }                 
            }

            // Function to handle the error event.
            // Prints the error message to the page.
            function errorHandler(data, errorCode, errorMessage) {
                alert(errorMessage);
            }           
        });

        // Function to retrieve a query string value.
        // For production purposes you may want to use
        //  a library to handle the query string.
        function getQueryStringParameter(paramToRetrieve) {
            var params =
                document.URL.split("?")[1].split("&");
            var strParams = "";
            for (var i = 0; i < params.length; i = i + 1) {
                var singleParam = params[i].split("=");
                if (singleParam[0] == paramToRetrieve)
                    return singleParam[1];
            }
        }
    </script>
3
  • Ty Lee_MSFT for reply. For JS files i used this method: sharepoint.stackexchange.com/questions/194636/… But I am getting an error in SP.search.js Uncaught TypeError: this.get_objectData is not a function – user79174 Oct 17 '18 at 7:24
  • If you're using SharePoint hosted add-in, reference the library in PlaceHolderAdditionalPageHead <script type="text/javascript" src="/_layouts/15/sp.search.js"></script> – Lee_MSFT Oct 17 '18 at 8:40
  • Is it possible to search by letters like if i searched for 'ab' then will it return me all the results related to that two letters? – user79174 Oct 18 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.