0

I am looking to query and display items from all of the lists that exist (and will exist in the future) on a specific subsite (SP 2013).

Below is what I have so far but it is not producing any alert, any help is greatly appreciated.

var siteUrl = '/mysite/mysubsite';

function retrieveAllListProperties() {

    //var clientContext = new SP.ClientContext(siteUrl);
    //var oWebsite = clientContext.get_web();
    //this.collList = oWebsite.get_lists();

    //clientContext.load(collList);

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();

    this.listInfoCollection = clientContext.loadQuery(collList);

    clientContext.executeQueryAsync(Function.createDelegate(this, 
this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var listInfo = '';

    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Created: ' + 
oList.get_created().toString() + '\n';
    }
    alert(listInfo);
}

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

4 Answers 4

0

Try this.

<script 
src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" 
type="text/javascript">
</script>
<script
type="text/javascript"
src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">
</script>
<script type="text/javascript">

var siteUrl = '/mysite/mysubsite';

// Load the required SharePoint libraries.
$(document).ready(function () {

    // The js files are in a URL in the form:
    // web_url/_layouts/15/resource_file
    var scriptbase =  siteUrl  + "/_layouts/15/";

    // Load the js files and continue to
    // the execOperation function.
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", execOperation);
        }
    );
});

// Function to execute basic operations.
function execOperation() {

    // Continue your program flow here.
    retrieveWebSiteProperties(siteUrl);

}

function retrieveWebSiteProperties(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();
    this.listInfoCollection = clientContext.load(collList);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded(sender, args) {

    var listInfo = '';

    var listEnumerator = this.collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Created: ' + 
oList.get_created().toString() + '\n';
    }
    alert(listInfo);

}

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

</script>

Complete basic operations using JavaScript library code in SharePoint

1
  • Thanks so much, this worked for me. Suppose I need to add a filter so that only certain lists are displayed, could I add that to the onQuerySucceeded?
    – seaandtea
    Commented Dec 18, 2018 at 14:18
1

I would suggest you use Search Rest api for this requirement, query so many lists would increase large number of requests to server.

To use search rest api in add-in.

You could check MSDN guideline or GithHub demo.

To get all listitems by search rest api(may need paging based on your data volume).

/_api/search/query?querytext='ContentClass="STS_ListItem"'

More about ContentClass property

http://avalagopi767.blogspot.com/2016/02/sharepoint-2013-keyword-query-kql.html

0

You have to register another delegated execution in your onQuerySucceeded() to retrieve list items for each list. Haven't tried, but might work

function onQuerySucceeded() {

    var listInfo = '';

    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Created: ' + 
oList.get_created().toString() + '\n';

        retrieveListItems(oList)
    }
    alert(listInfo);
}

function retrieveListItems(oList) {


    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
        '<RowLimit>10</RowLimit></View>' //or whatever criteria you want
    );
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded2), 
        Function.createDelegate(this, this.onQueryFailed2)
    ); 
}

function onQuerySucceeded2(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed2(sender, args) {
    alert('Request list items failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

You can also do this using Promises, async/await calls to the REST API if u like.

[siteUrl]/_api/web/webs (to get subsites)
[subwebs(n)Url]/_api/web/lists (to get subweb  lists)
[subwebs(n)Url]/_api/web/lists/getbyid(guid'[list(n)guid]')/items (to get subweb list listItems)
0

We can achieve your requirement by using Content Query web part, which will query the specified URL for all list items.

Configuration details available here - Content Query WP COnfiguration

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.