sharepointadd-insharepoint-onlinehosted-app

Access Data in SharePoint Hosted Add-in Web


I have the following problem. On a SharePoint Online Environment a SharePoint Hosted App is installed that stores it's data in a list. I need to get data form this list, but the web of the SharePoint Hosted App does not allow any type of access outside of the very same app web / app context.

Is there any solution known for this? I know that the protection of the app web is there for a reason, but I'm wondering if there is really no way that a Sharepoint Hosted Add-In can share Data outside of it's context?


Solution

  • // Get Context of App Web 
    var context = new SP.ClientContext("<<SharePoint App Web Url>>");
    //Set Proxy web to get data of parent web apps
    var factory = new SP.ProxyWebRequestExecutorFactory("<<SharePoint App Web Url>>");
    context.set_webRequestExecutorFactory(factory);
    //get parent web context
    var appContextSite = new SP.AppContextSite(context, "<<SharePoint App Host Web Url>>");
    web = appContextSite.get_web();
    
    //get parent web list 
    var lists = appContextSite.get_web().get_lists().getByTitle("<<List Name>>");
    var caml = new SP.CamlQuery();
    //Ceate CAML Query to get items from list
    caml.set_viewXml('View><Query></Query></View>');
    var listItems = lists.getItems(caml);
    //load context of listItems
    context.load(listItems);
    context.executeQueryAsync(function () {
        var listInfo = '';
        var listEnumerator = listItems.getEnumerator();
        while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();
            listInfo += ' ID: ' + oList.get_id().toString() + '\n';
        }
        alert(listInfo);
        //here you can get list data 
    },
    function (sender, args) {
        console.log(args.errorMessage);
    });