Today I came across a requirement to filter the oob calendar(webpart) based on querystring parameter.For eg. If QueryString parameter  Field1 = “XYZ” than calendar page should show only event which is having Field1=”XYZ”In order to achieve above requirement using JavaScript, we need to override method of Calendar.js



Code Snippet

//Start Execution 
// This method customize calendar events. 
function CustomizeCalendarEvents() { 
var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;   
SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function($p0, $p1) {  

// Get querystring parameter value
Var fieldValue =
// Get all items from calendar list which is having Field1 = fieldValue and store item id in itemCollection Array using ECMA/web service
itemIdCollection = GetItemsfromList(fieldValue); 

// This function identifies item to hide on the page.

 itemToHide   =   GetItemsToHide($p0); 

// Remove selected items from array ($p0).
for(var count=0; count < itemToHide.length; count++)
   for(var flag=0 ; flag<$p0.length; flag++)
if($p0[flag].$12 == itemToHide[count])   
}  }
 // Call this method to render events on the page., $p0, $p1); 
catch (err) {
    txt = "There was an error on this page.\n\n";
    txt += "Error description: " + err.message + "\n\n";
    txt += "Click OK to continue.\n\n";
/ This function adds items to hide in array.
 function GetItemsToHide($p0) 
for(var i=0; i<$p0.length; i++)   
var itemId = $p0[i].$12; 
var result = IsItemExist(itemId,itemIdCollection);
     if(result == -1)   

// This function removes item from array.
function removeItemByIndex(arr, index)
arr.splice(index, 1);
  // This function checks element exist in array or not.
 function IsItemExist(elem, array)
var len = array.length;      

for (var i = 0; i < len; i++)
if (array[i] == elem)
        return i;
return -1;  



Code Explanation



  1. The very first function calls custom function when calendar.js is loaded completely.



  1. Inside try we are overriding calendar method.

 $p0 is an array containing all events to be rendered on the page. So you can modify this array according to your need for eg. Show selected events from this array and remove un-wanted events and supply it to, $p0, $p1); method


  1. $p0 Contents :

          a)      Event ID :  event id is available in variable  ‘$12”. It can be accessed by following below syntax


        Here is complete elements of array $p0



Calendar  before Applying Solution

Calendar  After Applying Solution


Usage Scenarios


1)      Filter calendar events based on query string in Url.

2)      To apply color coding to specific events based on condition.

3)      Limit number of events per day/month.

4)      It can control basic functionalities of calendar events.