Sharepoint Calendar WebPart - A Different Approach Pt 2
Written 05/03/12Return To Part 1

I had to first figure out how to show the events on the given time line. This brought me to the jQuery hoverIntent plugin by Brian Cherne. This is the same exact syntax as using jQuery's hover function except one crucial thing.

                                        $('.ms-acal-summary-dayrow td').hoverIntent( config );
                                        var config = {
			                                over: DisplayCalendar,
			                                interval: 300,
			                                timeout: 100000,
			                                out: CloseCalendar
		                                }
                                
  1. Line 1: This is initialized on page load (document.ready()). This says, "take all the td's (days on the calendar), and apply the hoverIntent feature to them, passing config".
  2. Lines 2 - 7:
    • The over parameter is used for the function ran for hovering over the element (mouseover).
    • The out parameter is used for the function ran for hovering out (mouseout).
    • The interval parameter says, "how long should I wait to fire the over function?". If the mouse is moved away from the element, the interval starts over once the mouse is over another element that fires the plugin. This is in milliseconds.
    • The timeout is used before the out function is called. This is in milliseconds. The reason I used such a high number is because my absolute positioned div is only so high, and I did not want it to grow if we had a lot of events in our calendar. Therefore I added a scroll bar, so the user can take the mouse of the hovered element and scroll through if desired. If the timeout amout was set lets say to zero (0), the user would not be able to scroll because the absolute div would be hidden on mouseout.

I followed the same approach as my last post,
SharePoint, Setting the Status Bar Notifcation From a List.
I will describe what is happening below.

		                            window.setInterval(function () {
			                            $('.ms-acal-summary-dayrow td').hoverIntent( config );
		                            }, 500);
										
		                            $('#calendarClose').click( function () {
		                                  $('#calendarDisplay').hide();
		                            });
		
		                            var config = {
			                            over: DisplayCalendar,
			                            interval: 300,
			                            timeout: 100000,
			                            out: CloseCalendar
		                            }
		
		                            $('.ms-acal-summary-dayrow td').hoverIntent( config );
				
		                            function DisplayCalendar() {			
			                            $('.middleTD').filter( function () {
				                               $(this).html('');
			                            });
	
			                            $('#calendarDisplay').addClass('calendarDisplayCSS').show();
		    
			                            // get date from CalendarWP - add to top header of table
			                            var date = $(this).attr('date');
			                            $('#calendarDate').text(date);
			     
			                            date = Date.parse(date).toString('yyyy-MM-dd');
			
			                            var soapEnv =
				                            "
                                             \
				                             \
				                             \
				                            CorporateCalendar \
				                             \
				                             \
				                             \
				                             \
				                             \
				                            500 \
				                             \
				                             \
				                            ";
	
			                            // Call web service
			                            $.ajax({
				                            url: "http://YourServer/_vti_bin/lists.asmx",
				                            type: "POST",
				                            dataType: "xml",
				                            data: soapEnv,
				                            complete: test,
				                            contentType: "text/xml; charset=\"utf-8\""
			                            });
				
			                            function test(xData, status) {
				                            //alert(xData.responseText); return false;
				                            var calendarList = [];	
				                            var startTime = "";
				                            var endTime = "";
				                            var calDate = "";
				                            var index = 0;
				                            var startTimeNum = "";
				                            var dateNum = "";
					                            $(xData.responseXML).find("z\\:row").each(function () {			
						                            // get date from calendar		    
					                                calDate = Date.parse($(this).attr("ows_EventDate")).toString('yyyy-MM-dd');

					                                // check if calendar date is equal to date of mouse hover
					                                if (calDate == date) {
							                            calendarData = {};
							                            calendarData.Title = $(this).attr("ows_Title");	
							
							                            // get time only from "EventDate" (start)					
							                            startTime = Date.parse($(this).attr("ows_EventDate").substring($(this).attr("ows_EventDate").indexOf(' '))).toString('h:mm tt');
							                            calendarData.StartTime = startTime;
							
							                            // get time only from "EndDate" (end)
							                            endTime = Date.parse($(this).attr("ows_EndDate").substring($(this).attr("ows_EndDate").indexOf(' '))).toString('h:mm tt');
							                            calendarData.EndTime = endTime;
							
							                            calendarList.push(calendarData);	
						                            }				
					                            });
										
					                            for (var i = 0; i < calendarList.length; i++) {
				                                startTimeMin = Date.parse(calendarList[i].StartTime).toString('mm');
					                            $('#cal-holder td').filter( function () {	
					                                  // startTime is 8:00am, 9:00am, etc.
					                                  if (startTimeMin == "00") {
					                                        startTime = Date.parse(calendarList[i].StartTime).toString('htt').toLowerCase();								
								
						      	                            if ($(this).html() == startTime) {
						      	                            endTime = Date.parse(calendarList[i].EndTime).toString('htt').toLowerCase();
						                                       $(this).next().append('
' + calendarList[i].Title + ' ' + startTime + " - " + endTime + '
'); } } // startTime is 8:30am, 9:30am, etc. else { startTimeNum = Date.parse(calendarList[i].StartTime).toString('htt'); dateNum = $(this).html().toUpperCase(); if (startTimeNum == dateNum) { startTime = Date.parse(calendarList[i].StartTime).toString('h:mmtt').toLowerCase(); endTime = Date.parse(calendarList[i].EndTime).toString('h:mmtt').toLowerCase(); $(this).parent().next().children().next().append('
' + calendarList[i].Title + ' ' + startTime + " - " + endTime + '
'); } } }); } } } function CloseCalendar() { $('#calendarDisplay').hide(); }
  1. Lines 1 - 3: This is rebinding the calendar to the hoverIntent plugin every .5 seconds. This was needed because if the user decided to switch months, then the hoverIntent plugin would not work for additional months.
  2. Lines 5 - 16: Closes the calendar (day viewer). Defines the functions needed for hoverIntent, and assigns the plugin to the month's days displayed in the Sharepoint calendar webpart.
  3. Lines 19 - 21: Clears the Calendar (day viewer).
  4. Lines 26 - 29: Grabs the date hovered and displays it in the day viewer.
  5. Lines 31 - 55: Ajax call to Sharepoint webservice. Replace "YourServer" with your Sharepoint website name. Replace line 36 with your Sharepoint Calendar List name. If your Sharepoint Calendar is very heavily populated, line 42's row limit may need to be higher.
  6. Lines 66 - 85: Iterates through each row of the xml data received from the Sharepoint webservice call to your Calendar List Name.
    • Lines 68 - 71: Get all dates that equal the date hovered on the calendar web part.
    • Lines 72 - 83: Gets the title (event name) and the start time/end time of the event. Adds this to the "calendarData" object, then adds to the array, "calendarList".
  7. Lines 87 - 117: Iterates through all items in the array, "calendarList", finds the appropriate place in the day viewer, and adds the event.

Here is the final look of the day viewer being populated by the events hovered over May 4th:

Sharepoint calendar webpart styled CSS
Tuesday, September 25 2012
-- 1 Comment --

Re:Complete Code for calendar webpart 08:50 AM by saggii

Thanks Rob. I redid the CSS but my code is always failing. If your is what it looks like in the post then it will solve problems of many.

Post Comment   

Check if you would like to receive e-mails for replies to this post




Posted by Rob Scott @ 08:57 AM Wednesday, September 26 2012  

I keep forgetting to look in the database for your email address. I end up always checking this at work and I don't have the IP for the DB. If you could email me your email address, I can send you updated code as I did w/ Alex. It's a lot different, and I assure you it works. as it's taken from my company's production box.

Thursday, September 27 2012
-- 1 Comment --

Calendar 02:43 AM by DevIT

Would love to see this updated code. It's a pretty slick looking calendar. Now I just have to wok out where to put code etc to try an get it all working Great article Thanks

Post Comment   

Check if you would like to receive e-mails for replies to this post




Posted by Rob Scott @ 11:20 PM Monday, October 15 2012  

I'll be posting a new blog for this updated code shortly. Sorry for the delay I've been super busy! Thanks

Tuesday, April 02 2013
-- 0 Comments --

Need your updated code 11:55 PM by sukumar

Hi, its very helpful, But please send the updated code to my email id.......... my email id is sukumarg.it2k06@gmail.com can you send your document how to implement the code...... Thanks....I am waiting for your reply

Post Comment   

Check if you would like to receive e-mails for replies to this post






Check if you would like to receive e-mails for replies to this post



2012

May ( 2 )
April ( 1 )
October ( 1 )

Anything in here will be replaced on browsers that support the canvas element