'use strict';

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");

function initializePage() { var context, appContext, hostWeb, user, lists, listItemCollection, projectList;

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {

    // Get hostweb and appweb string values
    var appWebUrl = window.location.protocol + "//" + window.location.hostname + _spPageContextInfo.webServerRelativeUrl;
    var hostUrl = _spPageContextInfo.siteAbsoluteUrl;

    // Create and access objects 
    context = new SP.ClientContext(appWebUrl);

    // Host web data access
    appContext = new SP.AppContextSite(context, hostUrl);
    hostWeb = appContext.get_web();
    lists = hostWeb.get_lists();
    projectList = lists.getByTitle('Projects');

    // Generate a CamlQuery 
    var camlQuery = new SP.CamlQuery();
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>'

// Get the items using the query
    listItemCollection = projectList.getItems(camlQuery);

// Load the object to browser
        Function.createDelegate(this, listSuccess),
        Function.createDelegate(this, listFail)

function listSuccess() {

function listFail(sender, args) {
    alert('Request failed. ' + args.get_message() +
    '\n' + args.get_stackTrace());


The browser loads the object but the collection seems to be empty. When i test this code on the success handler

function listSuccess() {

This will return the expected number of items. But when I try this code

firstItem = listItemCollection.getById(1);

This will run the error handler callback Finally, when I iterate using the enumerator


It will display a false meaning there are no items on the loaded collection. When I replace it with local app web list items and not host web list items, I can access them without any issues. One more thing, the program is located in App testing list for a development site so the url contains some random codes like https://{myhostname}-{randomcodehere}.sharepoint.com/sites/bm while the list items I am trying to access is located on the https://{myhostname}.sharepoint.com/sites/bm.

2 Answers 2


You should use var hostUrl = GetUrlKeyValue("SPHostUrl"); to get your host web url. _spPageContextInfo.siteAbsoluteUrl will give you the url of the app web not hostweb.

  • I tried displaying the var hostUrl and it does shows the correct target of https://{myhostname}-{random-code}.sharepoint.com/sites/bm. My question is, since this is in the testing phase and it has the random code in between. Is this hostUrl the same or points to the items on the actual https://{myhostname}.sharepoint.com?
    – Roy Selim
    Nov 8, 2016 at 2:33

hi try using iteration in success also check for AppManifest.xml add a READ permission for list and Web

var listItemEnumerator = listItemCollection.getEnumerator();

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('Title') + 
        '\nBody: ' + oListItem.get_item('Body');
  • Hi, thank you for the response. I am sorry I left this part missing. All the permissions are in place in the manifest file. I gave it FullControl.
    – Roy Selim
    Nov 8, 2016 at 2:30

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.