SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To get all the items in a List you use

// Setup context & load web
context = new SP.ClientContext.get_current();
web = context.get_web();

// Load task list
this.List = web.get_lists().getByTitle("YOUR_LIST_NAME);

// Create a query to get all items
var camlQuery = SP.CamlQuery.createAllItemsQuery();
this.ListItems = this.View.getItems(camlQuery);

// Call the server and pass callback functions
               Function.createDelegate(this, this.InitialLoad_OnSucccess),
               Function.createDelegate(this, this.OnFailure));

How do you get all the items in a View?

share|improve this question
up vote 18 down vote accepted

This could be done, but unfortunately only using two requests: first to retrieve view query text, second - to retrieve the items.

function getItemsFromView(listTitle, viewTitle)

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

        function(sender, args) {getItemsFromList(listTitle, "<View><Query>" + view.get_viewQuery() + "</Query></View>")},
        function(sender, args) {alert("error: " + args.get_message());}

function getItemsFromList(listTitle, queryText) 
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);

    var query = new SP.CamlQuery();

    var items = list.getItems(query);

            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext())
            alert("items retrieved: " + i);
        function(sender, args) {alert("error in inner request: " + args.get_message());}


// example of usage    
getItemsFromView("Tasks", "My Tasks");

P.S. The code is tested and works in my environment.

share|improve this answer
Outstanding, cheers! – Ryan Feb 6 '12 at 8:33

The following code should work:

  function runCode() {
      var clientContext = new SP.ClientContext();
      var targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
      var view = targetList.get_views().getByTitle("View Name");
      var query = new SP.CamlQuery();
      listItems = targetList.getItems(query);
      clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    function onQuerySucceeded() {
      var listEnumerator = listItems.getEnumerator();
      while (listEnumerator.moveNext()) {
        alert("Item containing ‘announce’ found! \nTitle: " + listEnumerator.get_current().get_item("Title"));

    function onQueryFailed(sender, args) {
      alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
share|improve this answer
Getting a "SP.ClientObject.checkUninitializedProperty" error on get_htmlSchemaXml – Ryan Feb 3 '12 at 13:30
You might want to ClientContext.load() that property. – Vardhaman Deshpande Feb 3 '12 at 13:53
clientContext.load(view,"HtmlSchemaXml") Didn't work, is that what you mean? Can you show what you mean please and have you had this code running? – Ryan Feb 3 '12 at 14:00

Since delegates cannot return values or populate anything anywhere in the script, they have NO value. Web services are the only option. This method can only display alerts to the user. Worthless!

share|improve this answer
Erm... what are you on about Jeff? Totally incorrect! – Ryan Nov 2 '12 at 17:04
Why do you think that, Clem? – bgmCoder Jul 20 '13 at 21:59
While you don't usually require a return value from a delegate, you still may reference and assign any available javascript objects (whether global or within closure scope) and functions from the body of any delegate. Just make sure it is executed. – Alexey Krasheninnikov Mar 24 '14 at 7:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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