3

Am trying to fetch single date events from SharePoint calendar list with the help of javascript in SharePoint hosted app

var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL
var myList = ctx.get_web().get_lists().getByTitle("MyList");
function retrieveBookingDetails() {
var Date ="09/01/2016";
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Where><eq><FieldRef Name=\'EventDate\'/><Value Type=\'DateTime\'IncludeTimeValue=\'TRUE\' >' + Date + '</Value></eq>" + "</Where></View>');
    var items = myList.getItems(query);

    ctx.load(items);
   );
    ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    function onQuerySucceeded() {
        ----
        -------
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }
}

but it returns all events of calendar list,infact it also gives all events without this query

query.set_viewXml('<View><Where><eq><FieldRef Name=\'EventDate\'/><Value Type=\'DateTime\'IncludeTimeValue=\'TRUE\' >' + Date + '</Value></eq>" + "</Where></View>');


so what i suppose to do if i wants to fetch events of any specific date?

0

2 Answers 2

1

Pass Date in format of

var date = new Date();
var formattedDate = date.ToString("yyyy-MM-ddTHH:mm:ssZ");

Then pass "formattedDate" in CAML query.

1

Check the Syntax of Your Query

Make sure you're using proper syntax in the CAML that you're putting in your View XML.

Usually CAML is case sensitive, so you should use capitalized <Eq>...</Eq> instead of <eq>...</eq>.

It also looks like your View XML is missing a <Query> element. When setting the View XML of a CAML Query object, you should include the outer <View> and <Query> elements in your XML, as in this example.

Check the Recurrence of the Events in Your Calendar

Regular CAML queries work to retrieve one-time events from a calendar, but you need to process recurring events differently, as detailed in this related answer.

For example, a recurring event may have a start date of two years ago and an end date many years in the future, but maybe the event itself only actually occurs on the third Tuesday of every month. If you just query the list and try to compare today's date against the start date and end date to see if they overlap, that recurring event will show up in your results (even though today is not the third Tuesday of the month).

In server-side code you can get around this by setting the ExpandRecurrence property to true on the SPQuery object used to query the list. However, as of SP2010 and SP2013, that property is not exposed on the equivalent JavaScript Object Model.

A workaround is to use the old-fashioned Lists.GetListItems web service.

1
  • i don't have much theoratical knowledge about caml query but this <Query></Query> was cousing some errors while i was using that in caml query in javascript but when i removed it then my code start working properly,and as am doing this for app here as mentioned in question if am not passing caml query then also it is returning the result of all events and if am passing query as mentioned in question then also it gives same result so what's wrong with that?,for now i have got rid on this by checking some conditions for retrieving data as per date
    – Madhav
    Sep 2, 2016 at 6:13

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.