Display Calendar Events From SharePoint Calendar Lists Using Rest API

Introduction

 
We will query the SharePoint calendar list using REST API and format the results using JavaScripts and CSS. We will then use Content Editor WebPart to display the result in a SharePoint page. This is a basic use of REST to get and display list items in SharePoint On-prem. When we are playing with date value than it gets a little interesting. Here is the result:
 
 
Steps
  1. Create a calendar list using the Calendar list template. Give it a name. I named it “MyOfficeEvent”.
  2. For writing simple Javascripts, SP designer is still my choice to go. So, let's open SP Designer and go to the site assets library of your SP site. Create a folder inside the site assets library, name it CalendarDisplay and create two files in there: calendar.js and calendar.css as shown:



  3. I created the calendar icon using PowerPoint. PowerPoint has become my number one option when it comes to creating icons. I named it calendar.svg and copied inside the CalendarDisplay folder in site assets.
Open calendar.js and paste this js code:
  1. < link rel = "stylesheet"  
  2. href = "/SiteAssets/CalendarDisplay/calendar.css" >  
  3.   
  4.  <  
  5.  script type = "text/javascript"  
  6. src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" > < /script>    
  7.   
  8.  <  
  9.  script type = "text/javascript" >  
  10.   
  11.  $(document).ready(function() {  
  12.   
  13.   var webUrl = _spPageContextInfo.webAbsoluteUrl;  
  14.   
  15.   GetCalendar(webUrl);  
  16.   
  17.  })  
  18.   
  19. function GetCalendar(webUrl) {  
  20.   
  21.  var today = new Date();  
  22.   
  23.  var RestUrl = webUrl + "/_api/web/lists(guid'DF5C2335-4083-4118-88CF-0CE73C3DCA1A')/items?$select*&$filter=EventDate gt datetime'" + today.toISOString() + "'" + '&$orderby=EventDate asc';  
  24.   
  25.  $.ajax({  
  26.   
  27.   url: RestUrl,  
  28.   
  29.   method: "GET",  
  30.   
  31.   datatype: "json",  
  32.   
  33.   headers: {  
  34.   
  35.    "accept""application/json;odata=verbose",  
  36.   
  37.   },  
  38.   
  39.   success: function(data) {  
  40.   
  41.    data = data.d.results;  
  42.   
  43.    console.log(data);  
  44.   
  45.    var monthNames = ["Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sept""Oct""Nov""Dec"];  
  46.   
  47.    var dayNames = ["Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"];  
  48.   
  49.    $.each(data, function(index, value) {  
  50.   
  51.     var itemLink = webUrl + '/Lists/MyOfficeEvent/DispForm.aspx?ID=' + value.ID + '&Source=' + webUrl;  
  52.   
  53.     var title = value.Title || " ";  
  54.   
  55.     var d = new Date(value.EventDate);  
  56.   
  57.     var startDate, startMonth, startTime, endTime;  
  58.   
  59.     var fAllDayEvent = value.fAllDayEvent;  
  60.   
  61.     //check if all day event    
  62.   
  63.     if (!fAllDayEvent)  
  64.   
  65.     {  
  66.   
  67.      startDate = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();  
  68.   
  69.      startDate = "<span class='eventDay'>" + startDate + "</span>";  
  70.   
  71.      startMonth = "<span class='eventMonth'>" + monthNames[d.getMonth()] + "</span>";  
  72.   
  73.      startDay = dayNames[d.getDay()];  
  74.   
  75.      startTime = formatAMPM(d);  
  76.   
  77.      endDate = new Date(value.EndDate);  
  78.   
  79.      endDate = formatAMPM(endDate);  
  80.   
  81.      startDate = "<div class='eventDate'>" + startMonth + startDate + "</div>";  
  82.   
  83.      startTime = "<span class='eventTime'>" + startDay + " " + startTime + " - " + endDate + "</span>";  
  84.   
  85.     } else  
  86.   
  87.     {  
  88.   
  89.      d.setTime(d.getTime() + (4 * 60 * 60 * 1000)); //all day events are parsed -4hours by JS so adding 4 hrs    
  90.   
  91.      startDate = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();  
  92.   
  93.      startDate = "<span class='eventDay'>" + startDate + "</span>";  
  94.   
  95.      startMonth = "<span class='eventMonth'>" + monthNames[d.getMonth()] + "</span>";  
  96.   
  97.      startDay = dayNames[d.getDay()];  
  98.   
  99.      startDate = "<div class='eventDate'>" + startMonth + startDate + "</div>";  
  100.   
  101.      startTime = "<span class='eventTime'>" + startDay + " - All Day " + "</span>";  
  102.   
  103.     }  
  104.   
  105.     var html = '<tr><td>' + startDate + '</td><td>' + '<a href="' + itemLink + '">' + '<div class="divTitle"><span class="eventTitle">' + title + "</span>" + startTime + '</a>' + '</td></tr>';  
  106.   
  107.     $('#calendarTable tbody').append(html);  
  108.   
  109.    });  
  110.   
  111.   },  
  112.   
  113.   error: function(xhr, status, error) {  
  114.   
  115.    console.log("****Error: GetCalendar Failed. Error Msg: " + error);  
  116.   
  117.   }  
  118.   
  119.  });  
  120.   
  121. }  
  122.   
  123. function formatAMPM(date) {  
  124.   
  125.  var hours = date.getHours();  
  126.   
  127.  var minutes = date.getMinutes();  
  128.   
  129.  var ampm = hours >= 12 ? 'PM' : 'AM';  
  130.   
  131.  hours = hours % 12;  
  132.   
  133.  hours = hours ? hours : 12; // the hour '0' should be '12'    
  134.   
  135.  minutes = minutes < 10 ? '0' + minutes : minutes;  
  136.   
  137.  var strTime = hours + ':' + minutes + ' ' + ampm;  
  138.   
  139.  return strTime;  
  140.   
  141. }  
  142.   
  143. <  
  144. /script>    
  145.   
  146. <  
  147. div class = "wrapper" >  
  148.   
  149.  <  
  150.  div class = "headerDiv" > < img src = "/SiteAssets/calendardisplay/calendar.svg"  
  151. alt = "" > < span class = "headerSpan" > Office Events < /span></div >  
  152.   
  153.  <  
  154.  table id = "calendarTable" >  
  155.   
  156.  <  
  157.  tbody > < /tbody>    
  158.   
  159.  <  
  160.  /table>    
  161.   
  162.  <  
  163.  div class = "divViewAll" > < a href = "/Lists/MyOfficeEvent/calendar.aspx" > View All < /a></div >  
  164.   
  165.  <  
  166.  /div>    
Note
In the REST query, I am using a filter query to get only future events and sorting them by ascending order. Function formatAPPM is used to format start time and end time.
 
I am using if(fAllDayEvent) condition to check if the event is an all-day event or not. Otherwise, we will see some mismatch in the start and end time of all-day events as shown:
 
 
Open calendar.css and paste this CSS code:
  1. .wrapper {  
  2.     width30 % ;  
  3. }  
  4.   
  5. .headerDiv {  
  6.     font - family: "Open Sans""Helvetica Neue"HelveticaArial, sans - serif;  
  7.     background - color#d7d7da;  
  8.     width100 % ;  
  9.     padding - left: 12 px;  
  10.     font - size17 pt;  
  11.     color#494950;    
  12.  }    
  13.     
  14. .headerDiv img{    
  15.  width40px;    
  16.  vertical-alignmiddle;    
  17.  margin-bottom5px;    
  18.  padding-left:5px;    
  19. }    
  20.     
  21. .headerSpan{    
  22.  padding-left:10px;    
  23.  line-height:45px;    
  24. }    
  25.     
  26. .divTitle{    
  27.  padding-left:5px;    
  28. }    
  29.     
  30. .eventTitle{    
  31. display:block;    
  32. }    
  33.     
  34. .eventTime{    
  35.  display:block;    
  36.  font-size:9pt;    
  37.  margin-top:3px;    
  38.  }    
  39.     
  40. .eventDay, .eventMonth{    
  41.  display:block;    
  42.  margin-bottom-5px;    
  43.  }    
  44.     
  45. .eventDate {    
  46.  width60px;    
  47. text-aligncenter;    
  48.  border-radius: 3px;    
  49.  padding4px 0px;    
  50.  }    
  51.     
  52. .eventDay{    
  53.  font-size20pt;    
  54.  }    
  55.     
  56. .eventMonth{    
  57.  font-size11pt;    
  58.  }    
  59.     
  60. .divViewAll{    
  61.  float:right;    
  62.  margin-right:20px;    
  63.  }     
Next, go to your choice of SP page and insert a content editor webpart. Edit the webpart and give the link to your Javascript file.
 
 
That’s it. You should see the final webpart on your page. If the webpart is not visible, check your console in the browser and let us know if there is any issue.