Method Chaining in CSOM

Posted on 5/8/2013 @ 2:52 AM in #SharePoint by | Feedback | 1888 views

The golden rule about client side code (or any distributed application for that matter), your communication should be chunky, not chatty.

JavaScript CSOM (Client Side Object model) in SharePoint 2013, has a construct called as method chaining that allows us to tersely specify all the stuff we would like to get in one shot.

Here is how,

   1:  var items = ctx
   2:          .get_web()
   3:          .get_lists()
   4:          .getByTitle('ListName')
   5:          .getItems('');
   6:   
   7:  var items = clientContext.loadQuery(items);
   8:   
   9:  clientContext.executeQueryAsync(function () {
  10:      items.forEach(function (item) {
  11:          // access item
  12:      })
  13:  })

See, now you can very easily see what is going on.

Sound off but keep it civil:

Older comments..