1

Am unable to retreive the list from the site. I have written the below code

var siteUrl = '/sites/dev/';  (or) 'complete site url'

function retrieveListItems() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('SitesList');
}

In this if i put complete site url am able to fetch the site. But not able to retreive the list. Any help would be great.

3

As you are using NAPA, I presume you want to access host web list from app. Apps execute in different domain (called app web) than host web.

The sample code is

    var context = new SP.ClientContext(appWebUrl);

    var appContextSite = new SP.AppContextSite(context, hostWebUrl);

    var list = appContextSite.get_web().get_lists().getByTitle('SitesList');

    context.load(list);
    context.executeQueryAsync(onSuccess, onFailure);

Get the appWebUrl and hostWebUrl from query string parameters SPAppWebUrl and SPHostUrl respectively.

For more details, please visit this link.

EDIT: To access host web data from app web, we don't need SP.RequestExecutor.js. Therefore, updated the code above. Check this link for more info. According to this link:

By default, a SharePoint-hosted app is allowed to issue cross-domain calls to the host web, provided that it has proper permissions.

-1
  1. You are using Napa you so you are into appWeb, Now it depend from where you need to Pull the data if your List is in appWeb you can use the below code

    var context = new SP.ClientContext(appWebUrl);

    var appContextSite = new SP.AppContextSite(context, hostWebUrl);

    var list = appContextSite.get_web().get_lists().getByTitle('SitesList');

    context.load(list); context.executeQueryAsync(onSuccess, onFailure);

And define your success and Faliure methods.

  1. If you List is in Hostweb then you need to use SP.RequestExecutor.js to make cross domain call.

Check the below sample code

     function getAllItems() {
var executor = new SP.RequestExecutor(appweburl);
var endpointUri = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('";
endpointUri += listTitle + "')/items?@target='" + hostweburl + "'";
executor.executeAsync(
    {
        url: endpointUri,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            var jsonObject = JSON.parse(data.body);

            var results = jsonObject.d.results;
            var myJsonObject = results;
            avm.displayEmpData(myJsonObject);
        },
        error: function (data, errorCode, errorMessage) {
            toastr.error("Something went Wrong : " + errorMessage);
        }
    }
);

}

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.