1

I have got CAML Query. How to count how many elements is in the list checked ?

Here is javascript code:

  var siteUrl = '/sites/MCUW-IT/kostka-pilotaze/restauracje-pilotaże/';
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Testowa Lista');
    //this.oListItem = oList.getItemById(j);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Contains><FieldRef Name="Connect"/><Value Type="LookupMulti">site</Value></Contains></Where></Query><RowLimit>5</RowLimit></View>');
    this.listItems = oList.getItems(camlQuery);

    clientContext.load(listItems);

1 Answer 1

0

To get total no.of items in a SharePoint List no need to insert query xml,

var siteUrl = '/sites/MCUW-IT/kostka-pilotaze/restauracje-pilotaże/';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle("Testowa Lista");

var camlQuery = new SP.CamlQuery();
this.listItems = oList.getItems(camlQuery);

clientContext.load(listItems);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), Function.createDelegate(this, this.onQueryFailed));


function onListItemsLoadSuccess(sender, args) 
{
    var count = 0;
    count=this.listItems.get_count();
    alert("Total Count: "+count);

    var listItemInfo = '';
    var listItemEnumerator = listItems.getEnumerator();

    while (listItemEnumerator.moveNext()) 
    {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) 
{
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}


If you are trying to get no.of items based on a condition (example: Title ='SampleTitle') then use CAML query, Add query xml

camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>SampleTitle</Value></Eq></Where></Query></View>");

before this line,

this.listItems = oList.getItems(camlQuery);

Also refer this Retrieve List Items

7
  • But i must selected - checed elements, so I must have ID of element.
    – Grzegorz Z
    Commented Aug 21, 2018 at 13:06
  • And after this looped each of element.
    – Grzegorz Z
    Commented Aug 21, 2018 at 13:07
  • Vishnu I need a selected items, not only count all items in a list, but selected.
    – Grzegorz Z
    Commented Aug 22, 2018 at 7:28
  • 1
    @GrzegorzZ I have updated the answer, please check
    – Vishnu S
    Commented Aug 22, 2018 at 8:19
  • Still Vishnu if I put the code to the button i can't fired the code. I click the button and nothing happening.
    – Grzegorz Z
    Commented Aug 24, 2018 at 11:54

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.