1

When creating a provider hosted app/add-in, how would you access a list located on a sub-site of the page it was installed on? I can access lists at the top-level of the Host web using the below syntax, but can't figure out the syntax to get any other lists from the Host web that have been created in sub-sites of the Host web. No matter how I alter the path's, I can't get it to bring in these lists, just constant 404 errors.

// To get list information from the top-level
var getQueryStringParameter = function (p) {
                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] == p)
                        return singleParam[1];
                }
            };

            var hostweburl;
            var appweburl;

            // Load the required SharePoint libraries
            $(document).ready(function () {
                //Get the URI decoded URLs.
                hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
                appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

                // resources are in URLs in the form:
                // web_url/_layouts/15/resource
                var scriptbase = hostweburl + "/_layouts/15/";

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

            function execCrossDomainRequest() {
                var executor = new SP.RequestExecutor(appweburl);
                executor.executeAsync(
                    {
                        url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Goals')/items?@target='" + hostweburl + "'",
                        method: "GET",
                        headers: { "Accept": "application/json; odata=nometadata" },
                        success: retrievedGoalsList,
                        error: errorGettingGoalsList
                    }
                );

                // Success function
                function retrievedGoalsList(data) {
                    // This returns list information successfully if list is in the Host web
                }

                // Error function
                function errorGettingGoalsList(err) {
                    console.log("Async call failed with error: ", err);
                }                
            }

1 Answer 1

1

So the above code actually works just fine, but to update the path you need to update the actual hostweburl variable directly and not just manually update the url path inside the async call. For instance:

// Successful method
// Prior to the execCrossDomainRequest call
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl")) + "/subsite";

// Failed method
// Inside the execCrossDomainRequest call
url: appweburl + "/_api/SP.AppContextSite(@target)/subsite/web/lists/getbytitle('Goals')/items?@target='" + hostweburl + "'",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.