I've got an SharePoint Hosted Add-in written in Visual Studio 2013 that has a web part that is deployed to a SharePoint Office 365 site. The web part surfaces fine on the page but when I try to get list titles from the site I get an error about the contextinfo.
I've set the AppManifest.xml to have the following Permissions
Scope: Web
Permission: Manage
Here is the script code in the web part .aspx file
$(document).ready(function ()
{
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
loadDependentScripts();
});
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];
}
}
function loadDependentScripts()
{
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function ()
{
$.getScript(scriptbase + "SP.js",
function () { $.getScript(scriptbase + "SP.RequestExecutor.js", getHostWebListsUsingREST); }
);
}
);
}
function getHostWebListsUsingREST()
{
var executor;
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
{
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/?@target='" + hostweburl + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: onGetHostWebListsUsingRESTSuccess,
error: onGetHostWebListsUsingRESTFail
}
);
}
function onGetHostWebListsUsingRESTSuccess(data)
{
var jsonObject = JSON.parse(data.body);
var lists = jsonObject.d.results;
var listsHtml = $.each(lists, function (index, list)
{
$('#lists').append(list.Title + "(" + list.ItemCount + ")<br />");
});
}
function onGetHostWebListsUsingRESTFail(data, errorCode, errorMessage)
{
alert('Failed to get host site. Error:' + errorMessage);
}
HTML nothing fancy
<body>
<div>
<p id="message">
<!--
initializing...
</p>
</div>
<div>
<p id="results">
</p>
</div>
</body>
The errors I'm getting are as follows in the 'sp.runtime.js'
Unhandled exception at line 2, column 60952 in https://myhost.sharepoint.com/_layouts/15/sp.runtime.js
0x800a138f - JavaScript runtime error: Unable to set property 'https://myhost.sharepoint.com/sites/Developer/Ven/TestSharePointAppPart/Pages/TestSharePointAppPart/_api/contextinfo' of undefined or null reference
And the alert from the script says
'Failed to get host site: Error:Forbidden'
What I have tried:
I've tried different permissions in the AppManifest.xml and I've got permissions on my login to create anything in the site.