Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

To get all the items in a List you use

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

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

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

// Call the server and pass callback functions
context.executeQueryAsync(
               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
add comment

3 Answers

up vote 9 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);
    context.load(view);

    context.executeQueryAsync(
        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();
    query.set_viewXml(queryText);

    var items = list.getItems(query);

    context.load(items);
    context.executeQueryAsync(
        function()
        {
            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext())
            {
                i++;
            }
            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
1  
Outstanding, cheers! –  Ryan Feb 6 '12 at 8:33
add comment

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
1  
Erm... what are you on about Jeff? Totally incorrect! –  Ryan Nov 2 '12 at 17:04
    
Why do you think that, Clem? –  BGM 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 at 7:07
add comment

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();
      query.set_viewXml(view.get_htmlSchemaXml());
      listItems = targetList.getItems(query);
      clientContext.load(listItems);
      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
1  
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
add comment

Your Answer

 
discard

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.