从特定列表的视图访问值

时间:2019-05-05 09:43:44

标签: javascript jquery sharepoint-2013 sharepoint-designer

如何使用JQuery或JavaScript获取特定列表中View的值?

1 个答案:

答案 0 :(得分:0)

    function getItemsFromView(listTitle, viewTitle) {

    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    context.load(list);
    var view = list.get_views().getByTitle(viewTitle);
    context.load(view);

    context.executeQueryAsync(
        function (sender, args) {
            var query = new SP.CamlQuery();
            query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

            var items = list.getItems(query);

            context.load(items);
            context.executeQueryAsync(
                function () {
                    var listEnumerator = items.getEnumerator();
                    return listEnumerator; // Return List Item array
                },
                function (sender, args) { alert("error in inner request: " + args.get_message()); }
        );
        },
        function (sender, args) { alert("error: " + args.get_message()); }
    );
}

//Example of usage    
var listItems = getItemsFromView("Tasks", "My Tasks");
var i = 0;
while (listEnumerator.moveNext()) {
    i++;
}
alert("items retrieved: " + i);

以下是一个类似的问题供您参考:

JavaScript library to get list items based on a view using CSOM/JSOM