0

I am trying to do a simple search using javascript in SharePoint online but I am getting the following error:Object doesn't support this action.

The error is thrown right here: var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);

I am using the code from GitHub

function getSearchResultsUsingREST(queryText) {
    // search can use the app web URL as the base, so no need to use SP.AppContextSite(@target) to access the host web..
    var searchUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'";
    var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);
    executor.executeAsync(
        {
            url: searchUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetSearchResultsSuccess,
            error: onGetSearchResultsFail
        }
    );
}

function onGetSearchResultsSuccess(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    if (results.length == 0) {
        $('#related-content-results').text('No related documents were found');
    }
    else {
        var searchResultsHtml = '';
        $.each(results, function (index, result) {
            searchResultsHtml += "<a target='_blank' href='" + result.Cells.results[6].Value + "'>" + result.Cells.results[3].Value + "</a> (" + result.Cells.results[10].Value + ")<br />";
        });
        $('#related-content-results').html(searchResultsHtml);
    }
}

function onGetSearchResultsFail(data, errorCode, errorMessage) {
    $('#related-content-results').text('An error occurred whilst searching for related content - ' + errorMessage);
}
0

you need to load the JS script first before you execute your function:

$.getScript(scriptbase + "SP.RequestExecutor.js", getSearchResultsUsingREST);
});

function getSearchResultsUsingREST(queryText) {
    // search can use the app web URL as the base, so no need to use SP.AppContextSite(@target) to access the host web..
    var searchUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'";
    var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);
    executor.executeAsync(
        {
            url: searchUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetSearchResultsSuccess,
            error: onGetSearchResultsFail
        }
    );
}

function onGetSearchResultsSuccess(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    if (results.length == 0) {
        $('#related-content-results').text('No related documents were found');
    }
    else {
        var searchResultsHtml = '';
        $.each(results, function (index, result) {
            searchResultsHtml += "<a target='_blank' href='" + result.Cells.results[6].Value + "'>" + result.Cells.results[3].Value + "</a> (" + result.Cells.results[10].Value + ")<br />";
        });
        $('#related-content-results').html(searchResultsHtml);
    }
}

function onGetSearchResultsFail(data, errorCode, errorMessage) {
    $('#related-content-results').text('An error occurred whilst searching for related content - ' + errorMessage);
}

https://msdn.microsoft.com/en-us/library/office/dn735924.aspx?f=255&MSPPError=-2147217396

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.