0

I'm deploying a SharePoint Hosted Add in in O365. I'm doing a simple call like this in my App.js:

var hostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));

function searchSites(webUrl, success, failure) {
    var url = webUrl + "/_api/search/query?querytext='contentclass:sts_site'";
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.query);
        },
        error: function (data) {
            failure(data);
        }
    });
}


//print sites info
searchSites(hostUrl,
    function (query) {
        var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
        for (var i = 0; i < resultsCount; i++) {
            var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
            var siteUrl = row.Cells.results[6].Value;
            console.log(JSON.stringify(siteUrl));
        }
    },
    function (error) {
        console.log(JSON.stringify(error));
    }
);

But I keep getting this: "Access denied. You do not have permission to perform this action or access this resource."

In the App Manifest I have: enter image description here

0

Would you want you Host web to allow connections from any other location?
(in other words do you leave your house door open when you are out?)

Your Host and App Web are 2 different sites, so you can't do Cross Origin calls

Search for SP RequestExecutor, that beast makes it possible for your App Web to call the Host Web

https://dev.office.com/sharepoint/docs/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints


FYI

since IE9 , In modern programming your:

    var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
    for (var i = 0; i < resultsCount; i++) {
        var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
        var siteUrl = row.Cells.results[6].Value;
        console.log(JSON.stringify(siteUrl));
    }

is written as:

    var rows=query.PrimaryQueryResult.RelevantResults.Table.Rows.result;
    rows.forEach(function(row){
        var siteUrl = row.Cells.results[6].Value;
        console.log(JSON.stringify(siteUrl));
    });

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.