1

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.

ysfibm
  • 436
  • 2
  • 14
  • 30

3 Answers3

2

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.

Thriggle
  • 7,009
  • 2
  • 26
  • 37
  • thanks it works fine , so how can i apply it to current day , and day+1 Example : today is 16 august so - i wanna retrieve number of events of today witch the category is "Visit" , put it in a div section . - and retrieve number of events of day+1 witch the category is "Visit" – ysfibm Aug 16 '16 at 15:36
  • Do you have any recurring events in the calendar? If so, you might need to use the more complicated technique mentioned in [this answer](http://stackoverflow.com/questions/38876519/retrieve-events-number-from-sharepoint-calendar-using-js/38883928#38883928) to limit the results to a specified date range; if you *don't* have recurring events, you can filter against the EventDate and EndDate columns as normal. – Thriggle Aug 16 '16 at 15:59
  • @Thriggle that will work as long as the query doesn't hit the 5000 items limit and I don't think that op has put any indexes there – nizzik Aug 16 '16 at 16:04
  • @Thriggle no i dont have recurring events what i wanna do is to add condition on the current day and day+1 – ysfibm Aug 16 '16 at 16:06
  • See my updated answer with CAML for filtering on the date fields. – Thriggle Aug 16 '16 at 16:23
  • @nizzik That is a good point, although ysfibm has indicated that they're looking for a count of a *subset* of list items, not all the items in the calendar. – Thriggle Aug 16 '16 at 16:36
  • @Thriggle thanks i've tried your code on the console , and i have this error: VM693:34 Uncaught ReferenceError: eventDate is not defined – ysfibm Aug 16 '16 at 21:23
  • thanks it work just a fine for TodaysEvents , but it doesn't retrieve tomorrow events, Any solution will be very helpfull ? – ysfibm Aug 16 '16 at 23:29
  • I think you'll get the results you want if you switch the "Offset" in the CAML query from "1" to "2". Right now the query is looking for events that start before midnight (tomorrow morning) but we really want events that start before midnight on the morning after tomorrow. In the JavaScript, you can also change the number from 1 to 2 in this line: `tomorrow.setDate(tomorrow.getDate()+2);` – Thriggle Aug 17 '16 at 15:55
  • thanks for the help ,i've switched the "Offset" in the CAML query from "1" to "2" , also change tomorrow.setDate(tomorrow.getDate()+2); i still don't retrieve tomorrow events can you help me please ? – ysfibm Aug 17 '16 at 22:20
1

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

ysfibm
  • 436
  • 2
  • 14
  • 30
0

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());
});
nizzik
  • 826
  • 5
  • 18
  • I try your code and it doesnt work , i have a error : Uncaught TypeError: listItemCollection.get_itemCount is not a function it seems that is not correct – ysfibm Aug 16 '16 at 15:21
  • My badania you should have used the `get_itemCount` on the list object. I have editted code sample above – nizzik Aug 16 '16 at 15:57