Tuesday, June 2, 2015

SharePoint 2013 - Create an App to get lists using CSOM

'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();
var list = web.get_lists();

(function () {
    // This code runs when the DOM is ready and creates a context object which is
    // needed to use the SharePoint object model

    $(document).ready(function () {
        getListCount();
    });

    // This function prepares, loads, and then executes a SharePoint query to get
    // the current users information

  function getListCount() {
context.load(web);
context.load(list);
        context.executeQueryAsync(onGetListCountSuccess, onGetListCountFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name

    function onGetListCountSuccess() {

    var ernumerator = list.getEnumerator();
  while(ernumerator.moveNext())
  {
  var listname = ernumerator.get_current();
      alert(listname.get_title());
  }

  $('#message').text('Hello ' +list.get_count());
    }

    // This function is executed if the above call fails

    function onGetListCountFail(sender, args) {
        alert('Failed to get list count. Error:' + args.get_message());
    }
})();