0

I am trying to view all items on time between 08:00 to 17:00, I get only one item count even if I have more?

var fromTime = new Date(result);
            fromTime.setHours(8);
            var toTime = new Date(result2);
            toTime.setHours(17);
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><And><Geq><FieldRef Name=\'EventDate\'><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + fromTime.toISOString() + '</Value><FieldRef Name=\'EndDate\' /></Geq><Leq><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + toTime.toISOString() + '</Value></Leq></And></Where></Query></View>');


            this.collListItem = list.getItems(camlQuery);
            context.load(collListItem);

            var itemCount = collListItem.get_count();
            if (itemCount > 0) {
                alert(itemCount);
            }

item count gets only 1 when i have 2 items in the list why?

1
  • Martin, you can show me the values of these items in the list? Item 1: EventDate, EndDate. Item 2: EventDate, EndDate.
    – b1c10
    Dec 3, 2014 at 13:02

3 Answers 3

2

sorry, did not understand your question and noticed your code, you want to pick up items filtering EventDate and EndDate?

What is the value of fromTime.setTime() and toTime.setHours(17)?

trying to answer your question:

the format example: date to filter between 2014-12-02T08:00:00Z and 2014-12-02T17:00:00Z

camlQuery.set_viewXml("<View><Query><Where><And><Geq><FieldRef Name='date' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2014-12-02T08:00:00Z</Value></Geq><Leq><FieldRef Name='date' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2014-12-02T17:00:00Z</Value></Leq></And></Where></Query></View>");

before using this code checks FieldRef Name = 'date'.

I had search and test setHours(8 or 17) not work perfectly with toISOString() see the documentation here, Unfortunate not is possible, but i have logic for resolve this question.

Update(2):

var fromTime = new Date(result);
var fromtimeConverted = fromTime.toJSON().split('T')[0] + "T08:00:00Z";  //fromTime.setHours(8);
var toTime = new Date(result2);
var toTimeConverted = toTime.toJSON().split('T')[0] + "T17:00:00Z";//toTime.setHours(17);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Geq><FieldRef Name=\'EventDate\'><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + fromtimeConverted + '</Value><FieldRef Name=\'EndDate\' /></Geq><Leq><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + toTimeConverted + '</Value></Leq></And></Where></Query></View>');


this.collListItem = list.getItems(camlQuery);
context.load(collListItem);

var itemCount = collListItem.get_count();
if (itemCount > 0) {
    alert(itemCount);
}
12
  • fromTime and toTime is current date and time from the users input i am trying to user current date from the user add set his time to 8-17 alock, how could i use in the query fromTime.setHours(8) and toTime.setHours(17) together with toISOString() =?
    – Batistuta
    Dec 2, 2014 at 13:26
  • yes, you can convert this date to format YYYY-MM-DDTHH-MM-SSZ ?
    – b1c10
    Dec 2, 2014 at 13:32
  • Yes i know, but how do i do that using the things a asked in the question abow?
    – Batistuta
    Dec 2, 2014 at 13:33
  • Ok i will help you, pls wait, i will edit answer here.
    – b1c10
    Dec 2, 2014 at 13:40
  • Alright i only get the current added item and not all items from that date and range
    – Batistuta
    Dec 2, 2014 at 13:52
0

Your query is wrong. Try this

'<View><Query><Where><And><Gt><FieldRef Name=\'EventDate\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + fromTime.setTime() + '</Value><Gt><Lt><FieldRef Name=\'EndDate\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>' + toTime.setHours(17) + '</Value></Lt></And></Where></Query></View>'
2
  • I have tried with that also i only get current added item and not all items between the ranges
    – Batistuta
    Dec 2, 2014 at 13:15
  • i think maybe i need to use toISOString() how do i user that togher with setHours()?
    – Batistuta
    Dec 2, 2014 at 13:23
0

I'm sure your query is wrong, you are missing the where and query tag into it check this documentation: Retrieve List Items Using JavaScript

1
  • I have already tried with and without i only get current added item
    – Batistuta
    Dec 2, 2014 at 13:16

Your Answer

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

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