user165210
user165210

Reputation: 300

Search text in sharepoint by rest api

i trying to search query text in my shraepoint by with no luck. this is what i tried:

function search(searchQuery) {
    searchQuery = encodeURIComponent("'" + searchQuery + "'");

    var url = 'https://****.sharepoint.com/_api/search/query?querytext=' + searchQuery;
    $.ajax({
        url: url,
        headers: {
            "accept": "application/json;odata=nometadata",
        },
        method: "GET",
        success: function (data) {
            console.log(data);
        },
        error: function (jqxr, errorCode, errorThrown) {
            console.log(jqxr);
        }
    });
}

and i got in the console error 403:

{"odata.error":{"code":"-2147024891, System.UnauthorizedAccessException"

what can i do? when i enter to the url its working fine its gave me the xml

tnx

Upvotes: 0

Views: 231

Answers (1)

Lee
Lee

Reputation: 5493

You need run the script in SharePoint site so the request will use current user credential to call search rest api.

enter image description here

Upvotes: 1

Related Questions