1

I am retrieving lists using NAPA. The problem is that to whichever site or site collection I add the app, I always receive only

  • Composed Looks
  • Master Page Gallery

This is very few given that a C# or Powershell code ctx.Web.Lists usually returns ca. 10 default ones (Taxonomy, Solution Gallery, Assets, etc.). This leads me to think that the lists queried come from a site created by the app itself:

https://tenant-fced64b97322f5.sharepoint.com/sites/SiteCollection/SA181104/Pages/Default.aspx

I get the same results on all site collections. Full (almost) code:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();

function listperms()
{
context.load(lists);
context.executeQueryAsync(onlistItemsSuccess, onWebPropsFail);

}

function onlistItemsSuccess(sender, args)
{

var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
    // shows the title of the list
}

context.executeQueryAsync();
}

Questions:

  1. How to make the code retrieve all lists in a web?
  2. Why are there only 2 lists in the app site?
2

Every SharePoint app has its own SharePoint web associated with it. This is called the "App Web", and based on your code this is where the lists are coming from. Also, the SharePoint web on which the app was installed is usually known as "Host Web".

I don't know if you are trying to read the host web's list or the app web's lists, so here are an example for both.

App Web JSOM Code

var queryStringPairs = SP.ScriptHelpers.getDocumentQueryPairs();
var appweburl = queryStringPairs["SPAppWebUrl"];
var clientContext = new SP.ClientContext(appweburl);
var appWeb = clientContext.get_web();
var appWebLists = appWeb.get_lists();
clientContext.load(appWebLists);
clientContext.executeQueryAsync(onlistItemsSuccess, onWebPropsFail);

Host Web JSOM Code

var queryStringPairs = SP.ScriptHelpers.getDocumentQueryPairs();
var appweburl = queryStringPairs["SPAppWebUrl"];
var hostweburl = queryStringPairs["SPHostUrl"];
var clientContext = new SP.ClientContext(appweburl);
var proxyFac = new SP.ProxyWebRequestExecutorFactory(appweburl);
clientContext.set_webRequestExecutorFactory(proxyFac);
var appContextSite = new SP.AppContextSite(clientContext, hostweburl);
var hostWeb = appContextSite.get_web();
var hostWebLists = hostWeb.get_lists();
clientContext.load(hostWebLists);
clientContext.executeQueryAsync(onlistItemsSuccess, onWebPropsFail);

Also a sample project here

-Hope it helps

3
  • 1
    Pretty sure SP.ProxyWebRequestExecutorFactory is unnecessary in JSOM... despite many examples out there on the web using it. It's also possible to dynamically retrieve appweburl and hostweburl via SP.ScriptHelpers.getDocumentQueryPairs() which will give you a nice dictionary of all the querystring values as a JavaScript object. – John-M Jan 3 '15 at 0:05
  • Thanks for the answer. It is very helpful. Do you also know why there are only 2 lists in the app site? I'm guessing it's by design and no more are needed to run the app but it's a guess... – grisha Jan 3 '15 at 21:47
  • 2
    That is the way the app web is provisioned. If you need extra lists on the App's web, you can add them declaratively or programmatically from javascript. The App has full control over its own web. – Rodrigo Molinas Jan 3 '15 at 22:00

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.