20

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?

3 Answers 3

21

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.

2
  • 3
    Outstanding, cheers!
    – Ryan
    Feb 6, 2012 at 8:33
  • List Title may change!!! Use Guids instead.
    – Emaborsa
    Jul 3, 2018 at 7:30
3

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());
    }
4
  • Getting a "SP.ClientObject.checkUninitializedProperty" error on get_htmlSchemaXml
    – Ryan
    Feb 3, 2012 at 13:30
  • 2
    You might want to ClientContext.load() that property. Feb 3, 2012 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, 2012 at 14:00
  • For those who wonder - you need to do an executeQueryAsync to retrieve HtmlSchemaXml before accessing it via get_htmlSchemaXml.
    – Mx.
    Oct 10, 2016 at 9:37
-4

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!

3
  • 1
    Erm... what are you on about Jeff? Totally incorrect!
    – Ryan
    Nov 2, 2012 at 17:04
  • Why do you think that, Clem?
    – bgmCoder
    Jul 20, 2013 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. Mar 24, 2014 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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