Thursday, January 29, 2015

Retrieve List Items Using ECMA

The getItems(query) function enables you to define a Collaborative Application Markup Language (CAML) query that specifies which items to return. You can pass an undefined CamlQuery object to return all items from the list, or use the set_viewXml function to define a CAML query and return items that meet specific criteria.

The following example displays the ID, in addition to the Title and Body column values, of the first 100 items in the Announcements list, starting with list items whose collection ID is greater than 1.

    <script type="text/javascript">

        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
        // It will execute after SP.ClientContext is loaded
        function retrieveListItems() {
            var clientContext = new SP.ClientContext.get_current();
            var oList = clientContext.get_web().get_lists().getByTitle('Test Data');
            var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View/>');
            this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
        function onQuerySucceeded(sender, args) {
            var listItemEnumerator = collListItem.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current(); oListItem.get_item('YTD');
            }
        }


    </script>

No comments:

Post a Comment