Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

JavaScript
$(document).ready(function ()
{
    hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));

    loadDependentScripts();
});


// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
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/";

    // Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.Runtime.js",
        function ()
        {
            $.getScript(scriptbase + "SP.js",
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", getHostWebListsUsingREST); }
                );
        }
    );
}


function getHostWebListsUsingREST()
{
    var executor;

    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    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

HTML
<body>
    <div>
        <p id="message">
        <!-- The following content will be replaced with the user name when you run the app - see App.js -->
        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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900