Click here to Skip to main content
5,788,212 members and growing! (17,601 online)
Email Password   helpLost your password?
Enterprise Systems » SharePoint Server » Web Parts License: The Code Project Open License (CPOL)

Calendar Web Part for Sharepoint that displays & Give Details of Events from an Event List

By Kapoorisha

Custom Calendar WebPart , which highlights the dates with Events pulled from an Event List
Javascript, CSS, HTML, Ajax, ASP, ASP.NET

Posted: 27 Mar 2008
Updated: 27 Apr 2008
Views: 16,632
Bookmarked: 18 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 0.90 Rating: 1.89 out of 5
1 vote, 33.3%
1
2 votes, 66.7%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
New_CAL.JPG

Introduction

This is a calendar part which displays Events from an existing Event List(Dates higlighted),and also provide Details about the Events on click of the event Date.

Background

To Create this Webpart, I choose ASP.Net Calendar control and has overrided its OndayRender method to give colors to the dates with Events.

Sharepoint object model(Microsoft.sharepoint.dll) is then used to pull the Event Details,Displayed on the Click of the Date.

There are two Classes used:

1. A class to Highlight the Event dates and to Give colors to the dates of Calendar.(eg, Grey to weekends)

2. A Web-Part to Create and Render the Caleander control and to display the Event details Pulled from the Event List, On click of the Highlighted date.

Using the code

Steps to create Calendar Class:

1. Create a c# class library project in Asp.net.

2. Name it as WebPart_Calendar.

3. Name the Class to CustomCalendar.

4. Add libraries to the project,

System.web.UI.Webcontrols,

System.Drawing,

System.Data,

Microsoft.Sharepoint.dll and other if needed.

Use the following Code to give Colors to Weekend Dates,Dates of Othermonth and Dates With Events.

In the Code below, Events are pulled from an Event List named Team Calendar(You can change the List name).

 
namespace WebPart_Calendar
{
public class CustomCalendar : System.Web.UI.WebControls.Calendar
{
protected override void OnDayRender(TableCell cell, CalendarDay day)
{
if (day.IsToday)
{
cell.Attributes.Add("style", "font-weight:bold;background:#cee7ff;");
}
else if (day.IsWeekend)
{
cell.BackColor = System.Drawing.Color.LightGray;
}
else if (day.IsOtherMonth)
{
cell.BackColor = System.Drawing.Color.LightGray;
}
else
{
SPSite siteCollection = SPContext.Current.Site;
SPList calendarList = siteCollection.RootWeb.Lists["Team Calendar"];
// Construct a query that expands recurring events
SPQuery query = new SPQuery();
query.ExpandRecurrence = true;
query.CalendarDate = day.Date;
query.Query = "EventDate FieldName""\""" />""\""" />" + day.Date.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ") + "";

// Returns all items that would appear in the calendar view for the current day
SPListItemCollection calendarItems = calendarList.GetItems(query);

if(calendarItems.Count > 0){
    cell.BackColor = System.Drawing.Color.Khaki;
    cell.Attributes.Add("style", "font-weight:bold;");
            }

}
}
}


Build the Project to get WebPart_Calendar.dll

Steps to create a Webpart Class

1. Create a Webpart Templete Project(NEW-> project-> C#->Sharepoint->WebPart)

2. Name it SPWebPart_Calendar.

3. Add a Reference to some libraries,
System.web.UI.Webcontrols,
System.Drawing,
System.Data,
And,WebPart_Calendar.dll.


The Code Goes Below:

"""aspnet""">namespace SPWebPart_Calendar 
{ 
[Guid("7cd9a020-6a9d-4765-8138-ed85d648e553")] 
public class WebPart_Calendar_Class : System.Web.UI.WebControls.WebParts.WebPart 
{ 
CustomCalendar cal; 
Label lbl; 

public WebPart_Calendar_Class() 
{ 
this.ExportMode = WebPartExportMode.All; 
} 

protected override void Render(HtmlTextWriter writer) 
{ 
EnsureChildControls(); 
cal.RenderControl(writer); 
lbl.RenderControl(writer); 
} 

protected override void CreateChildControls() 
{ 
cal = new CustomCalendar(); 
this.cal.ID = "Calendar1"; 
this.cal.CellPadding = 5; 
this.cal.CellSpacing = 2; 
this.cal.BorderColor = System.Drawing.Color.LightGray; 
this.cal.TitleStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2f2f2"); 
this.cal.TitleStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6c6b6b"); 
this.cal.DayHeaderStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#818080"); 
this.cal.DayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#9f9e9e"); 
this.cal.OtherMonthDayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); 
this.cal.OtherMonthDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2f2f2"); 
this.Controls.Add(cal); 
lbl = new Label(); 
this.lbl.ID = "Label1"; 

this.lbl.Visible = false; 
this.lbl.EnableViewState = false; 
this.Controls.Add(lbl); 
this.cal.SelectionChanged += new EventHandler(cal_SelectionChanged); 
} 

protected void cal_SelectionChanged(object sender, EventArgs e) 
{ 
//this.lbl.Text = this.cal.SelectedDate.Date.ToShortDateString(); 
SPSecurity.CodeToRunElevated listEvents = new SPSecurity.CodeToRunElevated(delegate() { RenderEventsByDate(this.cal.SelectedDate); }); 
SPSecurity.RunWithElevatedPrivileges(listEvents); 
//RenderEventsByDate(this.cal.SelectedDate); 
this.lbl.Visible = true; 
} 

public void RenderEventsByDate(DateTime selectedDate) 
{ 
//Get the calendar events for that day 
SPSite siteCollection = SPContext.Current.Site; 

SPList calendarList = siteCollection.RootWeb.Lists["Team Calendar"]; 

// Construct a query that expands recurring events 
SPQuery query = new SPQuery(); 
query.ExpandRecurrence = true; 
query.CalendarDate = selectedDate; 
query.Query = "EventDate FieldName""\""" />""\""" />" + selectedDate.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ") + ""; 
query.ViewFields = " Fields to View fromEvent List";
SPListItemCollection calendarItems = calendarList.GetItems(query); 

foreach (SPListItem item in calendarItems) 
{ 
this.lbl.Text += item["Title"] + ": starts " + item["EventDate"].ToString() + " and ends " + item["EndDate"].ToString() + " 

"; 
} 
//If there are no events, display the message 
if (String.IsNullOrEmpty(this.lbl.Text)) 
this.lbl.Text = " There are no events for this date "; 

} 
} 
}

Finally, Deploy The WebPart as a Solution.

How to Deploy as solution Package : http://programmingsharepoint.blogspot.com/2008/03/deploy-webpart-as-solution-package.html

for More Details See : http://programmingsharepoint.blogspot.com/

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kapoorisha



Occupation: Software Developer (Senior)
Location: United States United States

Other popular SharePoint Server articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralAgregar un Calendar y asociar los eventos a una lista de SPmemberedudebolivar7:00 22 Oct '08  
GeneralOne Question About Calendar Types.memberhdv2127:11 9 Oct '08  
Generalcode not built due to error...memberKuldeep Kadyan3:22 6 May '08  
General[Message Deleted]membersubanboy6:29 7 May '08  
GeneralRe: code not built due to error...memberKuldeep Kadyan20:27 7 May '08  
GeneralRe: code not built due to error...memberedudebolivar6:59 22 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Apr 2008
Editor:
Copyright 2008 by Kapoorisha
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project