Blog, Development, Howto

How to call QuerySchedule using WebApi

Following code can be used to call QuerySchedule function using WebApi:

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}

var start = new Date(); //Put Start datetime here

var end = new Date(); //Put End datetime here
end.setDate(end.getDate() + 1);

var requestUrl = "/api/data/v8.2/QuerySchedule(ResourceId=@p1,Start=@p2,End=@p3,TimeCodes=@p4)";
requestUrl += "?@p1=" + context.getUserId().replace("{", "").replace("}", "");
//put Id of resource you want get data for as parameter 1

requestUrl += "&@p2=" + JSON.stringify(start).replace(/"/g, "");
requestUrl += "&@p3=" + JSON.stringify(end).replace(/"/g, "");
requestUrl += "&@p4=" + JSON.stringify(['0']);

//For TimeCodes use '0' for Available
//'1' for Busy
//'2' for Unavailable
//'3' for Filter

var req = new XMLHttpRequest();
req.open("GET", context.getClientUrl() + requestUrl, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;

        if (this.status === 200) {
            var results = JSON.parse(this.response);
            //you've got results to results variable
        } else {
            var errorText = this.responseText;
            //handle error here
        }
    }
};
req.send();

 

4 Comments

  1. Thanks for your answer.. It helped a lot. However I am getting resource with Id does not exist error. When I query the bookable resource entity with same id.. I get a valid result. Any idea why might I be getting this error??

    1. To be honest I have no idea what are you talking about. Can you please post what are you doing to initial thread on Dynamics Community Forum?

    1. Anasraf,
      I’m afraid I can’t help you. I believe something is wrong with your request/data that you send e.t.c.
      Andrew

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.