Skip to main content

Get Event List Items CAML Query

 // Set webUrl and listGuid to values specific to your site and list

var webUrl = "http://server/sitewhereyourlistexists";
var listGuid = "{000000000-0000-0000-0000-000000000000}"

// An XMLHttpRequest object is used to access the web service
var xhr = new XMLHttpRequest();
var url = webUrl + "/_vti_bin/Lists.asmx";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xhr.setRequestHeader("SOAPAction","http://schemas.microsoft.com/sharepoint/soap/GetListItems");

// The message body consists of an XML document 
// with SOAP elements corresponding to the GetListItems method parameters
// i.e. listName, query, and queryOptions
var data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
        "<soap:Body>" +
        "<GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" + 
                "<listName>"+listGuid+"</listName>" + 
                "<query>" + 
                    "<Query><Where>" +
                        "<DateRangesOverlap>" + 
                            "<FieldRef Name=\"EventDate\"/>"+
                            "<FieldRef Name=\"EndDate\"/>"+
                            "<FieldRef Name=\"RecurrenceID\"/>"+
                            "<Value Type=\"DateTime\"><Today/></Value>"+
                        "</DateRangesOverlap>"+
                    "</Where></Query>"+
                "</query>" +
                "<queryOptions>"+
                    "<QueryOptions>"+
                        "<ExpandRecurrence>TRUE</ExpandRecurrence>"+
                    "</QueryOptions>"+
                "</queryOptions>" +
        "</GetListItems>" +
        "</soap:Body>" +
    "</soap:Envelope>";

// Here we define what code we want to run upon successfully getting the results
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
        if(xhr.status == 200){
            var doc = xhr.responseXML;
            // grab all the "row" elements from the XML results
            var rows = doc.getElementsByTagName("z:row");
            var results = "Today's Schedule ("+rows.length+"):\n\n";
            var events = {};
            for(var i = 0, len = rows.length; i < len; i++){
                var id = rows[i].getAttribute("ows_FSObjType"); // prevent duplicates from appearing in results
                    if(!events[id]){ 
                        events[id] = true;
                        var allDay = rows[i].getAttribute("ows_fAllDayEvent"),
                            title = rows[i].getAttribute("ows_Title"),
                            start = rows[i].getAttribute("ows_EventDate"); 
                        var index = start.indexOf(" "); 
                        var date = start.substring(5,index)+"-"+start.substring(2,4); // get the date in MM-dd-yyyy format
                        start = start.substring(index, index+6); // get the start time in hh:mm format
                        var end = rows[i].getAttribute("ows_EndDate"); 
                        index = end.indexOf(" "); end = end.substring(index,index+6); // get the end time in hh:mm format
                        results += date + " " + (allDay == "1" ? "All Day\t" : start + " to " + end ) + " \t " + title + "\n";
                    }
                }
                alert(results);
            }else{
                alert("Error "+xhr.status);
            }   
    }
};

// Finally, we actually kick off the query
xhr.send(data);

Comments

Popular posts from this blog

Get Sum of Columns in a SharePoint list (Threshold edition)

It is a known fact that once the treshold limit is reached in SP, everything seems frozen.  At this point you have some options. Increase the limit using powershell or from the Central Admin OR Index the columns that you will like to operate on and create views based on those. However programmatically if you want to sum a list that has reached its threshold.  You can do so in batches as hown below:          protected   void  GetSummary( out   long  ideas,  out   long  votes,  out   long  comments,  out   long  transform)         {              long  ideasum = 0;              long columntoSum = 0;              long  commentS...

Add Web Visual Studio templates to SharePoint Project

Would you like to add the web user control to SharePoint 2007 project development.  Then open the project file with notepad and add {349c5851-65df-11da-9384-00065b846f21} to the ProjectTypeGUIDs node.   The necessary change is shown below: < ProjectTypeGuids > {349c5851-65df-11da-9384-00065b846f21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} </ ProjectTypeGuids >