0

I am trying to call SharePoint search API through JavaScript but cannot get it working.

I need to get all documents in a SharePoint site where Title='Test'. I am using SharePoint online.

Can someone suggest what is wrong here?

Here is the Ajax call:

var searchCriteria = "'"+"Title:Test"+"'";
var apiEndPoint = "https://server/sites/mysite/Shared%20Documents/Forms/AllItems.aspx/_api/search/query?querytext="+searchCriteria;


            console.log("before call");
            $.ajax({
                type: 'GET',
                crossDomain: true,
                async: false,
                url: apiEndPoint,
                headers: {
                    "Authorization": accessToken,
                    "Content-Type": "text/plain",
                    "Accept": "application/json;odata=verbose"
                },
                data: {
                    
                },
                success: function (data) {
                    $("#resultsDiv").append('<p style="color:green"> Files retrieved successfully. API Response is : </p>');
                    $("#resultsDiv").append(JSON.stringify(data));
                    console.log("response" + JSON.stringify(data));

                },
                error: function (data, errorThrown, status) {
                    $("#resultsDiv").append('<p style="color:red"> Error while retreieving file: ' +JSON.stringify(data)+'</p>');

                }
            });

I get "\r\n\r\n\r\n" in response and also a syntax error in Console logs at the ajax call:

Uncaught SyntaxError: Invalid or unexpected token at eval () at jquery-1.9.1.min.js:3 at Function.globalEval (jquery-1.9.1.min.js:3) at init.domManip (jquery-1.9.1.min.js:4) at init.append (jquery-1.9.1.min.js:4) at Object.success (SearchFileUsingMetadata2.html:118) at c (jquery-1.9.1.min.js:3) at Object.fireWith [as resolveWith] (jquery-1.9.1.min.js:3) at k (jquery-1.9.1.min.js:5) at r (jquery-1.9.1.min.js:5)

0

In the code provided by you, the apiEndPoint contains the path to the all document view URL. If you hit the same URL in the browser then it will not return any results as path before the "/_api" should be the web/site relative URL.

You can change the apiEndPoint like below and then check

var apiEndPoint = "https://server/sites/mysite/_api/search/query?querytext="+searchCriteria+";

Microsoft documentation for search API https://docs.microsoft.com/en-us/sharepoint/dev/general-development/sharepoint-search-rest-api-overview

3
  • Thanks. But when I change the path to be only till mysite. I get unauthorized access error. I have setup client id in Azure to have my app access to mysite and all other APIs work with that token. But somehow only search API returns unauthorized access. Jul 7 at 12:26
  • have you provided the search access while registering the app <AppPermissionRequests> <AppPermissionRequest Scope="sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" /> </AppPermissionRequests> Jul 7 at 14:58

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.