7

I have some code that returns all of the lists in my site however I need to only get lists that are using the discussion board template and I have hit a stumbling block with this.

I currently have the following:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', PreSaveAction);

function PreSaveAction() {

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

    context.load(lists);

    context.executeQueryAsync(Succeeded, Failed);


    function Succeeded() {

        var listItemEnumerator;

        listItemEnumerator = lists.getEnumerator();

        while (listItemEnumerator.moveNext()) {
            oListItem = listItemEnumerator.get_current();
            var listname = oListItem.get_title();
        }
    }

    function Failed(sender, args) {
        console.log("Failed" + args.get_message() + '\n' + args.get_stackTrace());
    }

}

I have tried a few different approaches but so far had no luck, I know that the list template ID is 108 and I have tried to use this to return the list I need but unfortunately I have not had any luck.

5

As you know the BaseTemplate of discussion board, so you can apply $filter in your all lists

/_api/Web/Lists?$filter=BaseTemplate eq 108

Make a GET request to the above URL, It will return all lists those are using discussion board template.

Example using jQuery

function getDiscussionBoardList() {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + '/_api/Web/Lists?$filter=BaseTemplate eq 108',
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(data) {
            console.log(data.d.results); // all discussion board lists are here
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
}

Using JSOM

In your code, you can apply some filter in Succeeded() callback. Meaning oListItem.get_baseTemplate() == 108

function Succeeded() {

    var listItemEnumerator;

    listItemEnumerator = lists.getEnumerator();
    var discussionBoardLists = [];

    while (listItemEnumerator.moveNext()) {
        oListItem = listItemEnumerator.get_current();
        if (oListItem.get_baseTemplate() == 108) {
            var listname = oListItem.get_title();
            discussionBoardLists.push(listname);
        }
    }

    // all discussion board list.
    console.log(discussionBoardLists);
}

In JSOM, you need to load all lists which unnecessary. In that case REST API is the best.

If you do not have jQuery, then you can use pure javascript XMLHttpRequest()

var xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET", _spPageContextInfo.webAbsoluteUrl + '/_api/Web/Lists?$filter=BaseTemplate eq 108');

xmlhttp.setRequestHeader("Accept", "application/json;odata=verbose");

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE) {
        if (xmlhttp.status == 200) {
            console.log(xmlhttp.responseText); // all discussion lists
        } else {
            console.log('Error: ' + xmlhttp.statusText)
        }
    }
}

xmlhttp.send();

So my recommendation is Only REST API for this requirement.

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.