ysfibm
ysfibm

Reputation: 436

Get list item count

I have a SharePoint calendar, with category of events ("Visit" , "Meeting"...) i wanna retrieve count of current day event witch the category is "Visit" . Here is my script :

var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Artdesk-calendrier");
    var caml = new SP.CamlQuery();
caml.set_viewXml("<View><Query>"+
    "<Where>"+
        "<And>"+
            "<Eq><FieldRef Name=\"Category\" /><Value Type=\"Choice\">Visite</Value></Eq>" +
            "<And>"+
                "<Leq><FieldRef Name=\"EventDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today Offset=\"1\"/></Value></Leq>"+
                "<Geq><FieldRef Name=\"EndDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today /></Value></Geq>"+
            "</And>"+
        "</And>"+
    "</Where>"+
"</View></Query>"); // empty query also works

var listItemCollection = list.getItems(caml);

clientContext.load(listItemCollection);
clientContext.executeQueryAsync(function() {
    var listItemEnumerator = listItemCollection.getEnumerator();
    var count = 0;
var eventsToday = [];
var eventsTomorrow = [];
var today = new Date();
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0);
var tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate()+1);
while (listItemEnumerator.moveNext()) {
    count++;
    var item = listItemEnumerator.get_current();
    var startDate = item.get_item("EventDate");
    var endDate = item.get_item("EndDate");
    var title = item.get_item("Title");
    if(endDate < tomorrow || eventDate < tomorrow){
        eventsToday.push(title);
    }else{
        eventsTomorrow.push(title);
    }
}
document.getElementById("TodaysEvents").innerHTML = eventsToday.join("\n");
document.getElementById("TomorrowsEvents").innerHTML = eventsTomorrow.join("\n");
document.getElementById("alerte1").innerHTML = count;
}, function(sender, args) {
  window.console && console.log(args.get_message());
});

It work just fine,but it retrieve me only the title of the last event of category "Visit" . What i want is : to retrieve the count of events of the current day which the category is "Visit".

Any help will be welcome.

Upvotes: 1

Views: 7623

Answers (3)

ysfibm
ysfibm

Reputation: 436

Hi iv'e found the solution to get day+1 count : i have change to and it work just fine , here's the whole script:

function getevents()
{ 
var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Artdesk-calendrier");
    var caml = new SP.CamlQuery();
caml.set_viewXml("<View><Query>"+
    "<Where>"+
        "<And>"+
            "<Eq><FieldRef Name=\"Category\" /><Value Type=\"Choice\">Visite</Value></Eq>" +
            "<And>"+
                "<Leq><FieldRef Name=\"EventDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today OffsetDays='1'/></Value></Leq>"+
                "<Geq><FieldRef Name=\"EndDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today /></Value></Geq>"+
            "</And>"+
        "</And>"+
    "</Where>"+
"</View></Query>"); // empty query also works

var listItemCollection = list.getItems(caml);

clientContext.load(listItemCollection);
clientContext.executeQueryAsync(function() {
  var listItemEnumerator = listItemCollection.getEnumerator();
  var count_ajrd = 0;
  var count_demain = 0;
var eventsToday = [];
var eventsTomorrow = [];
var today = new Date();
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0);
var tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate()+1);
while (listItemEnumerator.moveNext()) {

    var item = listItemEnumerator.get_current();
    var startDate = item.get_item('EventDate');
    var endDate = item.get_item('EndDate');
    var title = item.get_item('Title');
    if(endDate < tomorrow || startDate < tomorrow){
        eventsToday.push(title);
        count_ajrd++;
    }else{
        eventsTomorrow.push(title);
        count_demain++;
    }
}
document.getElementById("alerte2").innerHTML = eventsToday.join("\n");
document.getElementById("alerte1").innerHTML = eventsTomorrow.join("\n");
document.getElementById("todaynum").innerHTML = count_ajrd;
document.getElementById("tomorrownum").innerHTML = count_demain;
}, function(sender, args) {
  window.console && console.log(args.get_message());
});
}

Thanks @Thriggle for your help

Upvotes: 1

Thriggle
Thriggle

Reputation: 7049

Getting a Count of Items in your List Item Collection

You can either access the list item collection's count property directly as Nizzik suggested (using listItemCollection.get_count()), or do some simple math on your own, like below.

var count = 0;
while (listItemEnumerator.moveNext()) {
    count++;
}
document.getElementById("alerte1").innerHTML = count;

Limiting Your Results to Today's and Tomorrow's Events

If you don't have any recurring events to worry about, you can update your CAML query to filter the results, limiting to events that start tomorrow or earlier and which end today or later. Use the EventDate field to filter the "Start Date" and the EndDate field to filter the "End Date" (those are the internal names of those fields).

Your resulting CAML query might end up looking like this:

"<View><Query>"+
    "<Where>"+
        "<And>"+
            "<Leq><FieldRef Name=\"EventDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today Offset=\"2\"/></Value></Leq>"+
            "<Geq><FieldRef Name=\"EndDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today /></Value></Geq>"+
        "</And>"+
    "</Where>"+
"</View></Query>"

To also limit it to events with a Category equal to "Visite" as in the example code you provided, you could use the following CAML:

"<View><Query>"+
    "<Where>"+
        "<And>"+
            "<Eq><FieldRef Name=\"Category\" /><Value Type=\"Choice\">Visite</Value></Eq>" +
            "<And>"+
                "<Leq><FieldRef Name=\"EventDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today Offset=\"1\"/></Value></Leq>"+
                "<Geq><FieldRef Name=\"EndDate\"/><Value Type=\"DateTime\" IncludeTimeValue=\"False\"><Today /></Value></Geq>"+
            "</And>"+
        "</And>"+
    "</Where>"+
"</View></Query>"

To then differentiate between today's and tomorrow's events, you can check their EventDate and EndDate values from within the while loop as you enumerate through the results.

var count = 0;
var eventsToday = [];
var eventsTomorrow = [];
var today = new Date();
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0);
var tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate()+2);
while (listItemEnumerator.moveNext()) {
    count++;
    var item = listItemEnumerator.get_current();
    var startDate = item.get_item("EventDate");
    var endDate = item.get_item("EndDate");
    var title = item.get_item("Title");
    if(endDate < tomorrow || startDate < tomorrow){
        eventsToday.push(title);
    }else{
        eventsTomorrow.push(title);
    }
}
document.getElementById("TodaysEvents").innerHTML = eventsToday.join("<br/>");
document.getElementById("TomorrowsEvents").innerHTML = eventsTomorrow.join("<br/>");
document.getElementById("alerte1").innerHTML = count;

You might need to customize the logic depending on how you want to draw the line between "today's" events and "tomorrow's events," especially in weird situations such as when an event starts on one day and ends on another.

Upvotes: 2

nizzik
nizzik

Reputation: 826

When you get proper CAML query with filter on Current Date you can get the items number with get_itemCount() method on your list item collection after you load it with your query. Try following code

clientContext.executeQueryAsync(function() {
  window.console && console.log('Total Visits: ' + list.get_itemCount());
}, function(sender, args) {
  window.console && console.log(args.get_message());
});

Upvotes: 0

Related Questions