0

I have two different calendar views:

View Name is "All Events"

.../Lists/CalendarName/AllItems.aspx

View Name is "Current Events"

.../Lists/CalendarName/MyItems.aspx

I'm only able to query AllItems.aspx but I want to pull items from MyItems.aspx. How can I do this using JSOM or REST? The following code seems to still only pull from the "All Events" calendar view and not the "Current Events" view.

function queryCalendarList(listTitle, viewTitle, success, error) {
    var clientContext = new SP.ClientContext(url);
    var oList = clientContext.get_web().get_lists().getByTitle(listTitle);
    var view = oList.get_views().getByTitle(viewTitle);
    clientContext.load(view, 'ViewQuery');

    clientContext.executeQueryAsync(Function.createDelegate(this, function () {
        var viewQry = "<View><Query>" + view.get_viewQuery() + "</Query></View>";
        getItems(listTitle, viewQry, success, error);
    }), error);
}

function getItems(listTitle, queryText, success, error) {
    var ctx = new SP.ClientContext(url);
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var query = new SP.CamlQuery();
    query.set_viewXml(queryText);
    var items = list.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(
        function () {
            success(items)
        },
        error
    );
}

queryCalendarList("DD Calendar", "Current Events", function (items) {
        for (var i = 0; i < items.get_count(); i++) {
            var item = items.get_item(i);
            console.log(item.get_item('ID'))
            console.log(item.get_item('Title'));
            console.log(item.get_item('EventDate'));
            console.log("=======");
        }
    },
        function (sender, args) {
            console.log(args.get_message())
        });

1 Answer 1

0

The best way to get the list items from specific view is to first get the CAML query of the list view before and then fetch items from the list based on CAML query.

Check below references for complete code:

  1. Retrieve Items from a List View of a Sharepoint List using JavaScript
  2. Get list items from view using JavaScript (jsom) in SharePoint Online/2013/2016
  3. How to get specific listview data with javascript?
1
  • This doesn't seem to pull from my "Current Events" view (MyItems.aspx). It's still pulling from "All Events" view (AllItems.aspx). I edited my code snippet in my post.
    – Paul Vu
    Dec 8, 2020 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.