This conceptual tutorial shows you how to create a custom calendar web part that gets its events from an already-existing calendar in MOSS 2007.
Prerequisites:
- Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions
- MOSS 2007
- SharePoint Server 2007 SDK Software Development Kit
- VS.NET 2005
- ALL SP development is to be done on the MOSS 2007 server itself.
Note: This tutorial will be a conceptual one, not one where I go into the actual development of a real application/web part.
- Open up VS.NET, then create a new SharePoint-Web Part project.
- Make sure a reference to Microsoft.SharePoint.dll is there.
- Open up the Web Part .cs (code) file.
- Ensure the following using statements are present:
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages; - Ensure the Web Part class is deriving from the following class:
System.Web.UI.WebControls.WebParts.WebPart
- Something in the Web Part class code file, define variables that might be used, such as the following:
private String strListName = "DotNetFun Calendar"; private SPCalendarView calendarView = new SPCalendarView(); private Guid listGuid; - Override the base class' CreateChildControls() method:
protected override void CreateChildControls()
- The CreateChildControls() method should look something like this:
protected override void CreateChildControls() { try { SPSecurity.RunWithElevatedPrivileges( delegate() { base.CreateChildControls(); ViewType viewType = ViewType.Month; using (SPWeb web = SPContext.Current.Site.OpenWeb(this.webGuid)) { // Change Cal-view based on if QueryString specifies it: if (Page.Request.QueryString["CalendarPeriod"] != null) { switch (Page.Request.QueryString["CalendarPeriod"].ToString().ToLower()) { case "day": this.calendarView.ViewType = "day"; viewType = ViewType.Day; break; case "week": this.calendarView.ViewType = "week"; viewType = ViewType.Week; break; case "timeline": this.calendarView.ViewType = "timeline"; viewType = ViewType.Timeline; break; default: this.calendarView.ViewType = "month"; viewType = ViewType.Month; break; } } SPList listDotNetFunCalendar = web.Lists[this.strListName]; if (listDotNetFunCalendar.Views["Calendar"] == null) throw new ApplicationException("The Calendar view was not found. Please create one accordingly."); SPView viewDotNetFunCalendar = listDotNetFunCalendar.Views["Calendar"]; if (this.calendarView.ViewGuid != null) viewDotNetFunCalendar = listDotNetFunCalendar.Views[new Guid(this.calendarView.ViewGuid)]; this.listGuid = listDotNetFunCalendar.ID; DateTime dtStart = DateTime.Now; if (this.calendarView.SelectedDate != null) dtStart = DateTime.Parse(this.calendarView.SelectedDate).AddDays(-7); DateTime dtEnd = dtStart.AddMonths(1).AddDays(7); SPQuery query = new SPQuery(); query.Query = String.Format( " " + " ", "Start Time", dtStart.ToShortDateString(), dtEnd.ToShortDateString()); viewDotNetFunCalendar.Query = query.Query; web.AllowUnsafeUpdates = true; //viewDotNetFunCalendar.Update(); //System.Web.HttpContext.Current.Response.Write(String.Format("{0}" + " " + "" + " {1} " + "" + " {2} " + "
", viewDotNetFunCalendar.Query)); SPCalendarItemCollection items = new SPCalendarItemCollection(); foreach (SPListItem listItem in listDotNetFunCalendar.GetItems(viewDotNetFunCalendar)) { SPCalendarItem calItem = new SPCalendarItem(); calItem.ItemID = listItem["ID"].ToString(); calItem.Title = listItem["Title"].ToString(); calItem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian); calItem.StartDate = (DateTime)listItem["Start Time"]; calItem.ItemID = listItem.ID.ToString(); calItem.WorkSpaceLink = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); // Only allow color-coding, which requires all all-day // event, if a Month view: if (viewType == ViewType.Month) calItem.IsAllDayEvent = true; calItem.DisplayFormUrl = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); if (listItem["End Time"] != null) { calItem.hasEndDate = true; calItem.EndDate = (DateTime)listItem["End Time"]; } else calItem.hasEndDate = false; if (listItem["Description"] != null) calItem.Description = listItem["Description"].ToString(); if (listItem["Location"] != null) calItem.Location = listItem["Location"].ToString(); // Define CSS based on category: // Note, CSS styles are stored in CORE.CSS, here: // %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\STYLES // Append "sel" (without quotes) to the CSS Class name to // override the onmouseover CSS changes in the CORE.CSS file. switch (listItem["Category"].ToString()) { case "Programming/Scheduling": calItem.BackgroundColorClassName = "DotNetFunCalendarProgramming"; break; case "Production": calItem.BackgroundColorClassName = "DotNetFunCalendarProduction"; break; case "Ad Sales": calItem.BackgroundColorClassName = "DotNetFunCalendarAdSales"; break; case "Marketing/Promotions": calItem.BackgroundColorClassName = "DotNetFunCalendarMarketing"; break; case "PR": calItem.BackgroundColorClassName = "DotNetFunCalendarPR"; break; case "Online/Interactive": calItem.BackgroundColorClassName = "DotNetFunCalendarInteractive"; break; case "Company Events/HR": calItem.BackgroundColorClassName = "DotNetFunCalendarHR"; break; case "General": calItem.BackgroundColorClassName = "DotNetFunCalendarGeneral"; break; } items.Add(calItem); } // Set calendar-only properties: this.calendarView.DisplayItemFormUrl = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); this.calendarView.EditItemFormUrl = String.Format("/Lists/{0}/EditForm.aspx", this.strListName); this.calendarView.NewItemFormUrl = String.Format("/Lists/{0}/NewForm.aspx", this.strListName); this.calendarView.EnableViewState = true; this.calendarView.DataSource = items; this.calendarView.DataBind(); this.DataBind(); } this.Controls.Add(this.calendarView); }); } catch (Exception err) { this.HandleException(err); } } - Override the base class' RenderContents(HtmlTextWriter writer) method:
RenderContents(HtmlTextWriter writer)
- The RenderContents(HtmlTextWriter writer) method should look something like this:
protected override void RenderContents(HtmlTextWriter writer) { SPSecurity.RunWithElevatedPrivileges( delegate() { // Check if current user can add events: bool blnCanAddEvents = false; using (SPWeb web = SPContext.Current.Site.OpenWeb(this.webGuid)) { try { web.Lists[this.strListName].Permissions.CheckPermissions(SPRights.AddListItems); blnCanAddEvents = true; } catch { } } // Render header firstly: StringBuilder sbHeader = new StringBuilder(); sbHeader.AppendLine(" "); sbHeader.AppendLine("
"); writer.Write(sbHeader.ToString()); // Render calendar secondly: this.calendarView.RenderControl(writer); // Append color legend: StringBuilder sbLegend = new StringBuilder(); sbLegend.AppendLine(""); sbHeader.AppendLine("DotNetFun PROGRAMMING"); sbHeader.AppendLine(" "); sbLegend.AppendLine("
"); writer.Write(sbLegend.ToString()); }); }"); sbLegend.AppendLine("Legend: "); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine("
"); sbLegend.AppendLine(""); sbLegend.AppendLine("Options:
"); if (blnCanAddEvents) sbLegend.AppendLine(String.Format("- Add New Event
", this.strListName)); sbLegend.AppendLine(String.Format("- View the Full Calendar
", this.strListName)); sbLegend.AppendLine(String.Format("- RSS Feed
", this.listGuid.ToString())); sbLegend.AppendLine(String.Format("- Alert Me When this Calendar Changes
", this.listGuid.ToString())); sbLegend.AppendLine(" - Ensure that the AssemblyInfo.cs file is using whole, major version numbers and nothing else (e.g., 1.0.0.0).
- The code sample in this tutorial makes use of CSS via the MOSS 2007's CORE.CSS master CSS file, located here: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\STYLES
- Before you can use the Web Part, it must be trusted by declaring it a safe control. Do this by adding something like the following to the site's web.config file, typically, located here, C:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config:
- Add the following line to the AssemblyInfo.cs code file:
[assembly: System.Security.AllowPartiallyTrustedCallers()] - Sign the project with a strong-name key, then add it to the GAC, after compiling it, of course.
- Go to the settings page of the site on which the calendar list exists and from which data is being retrieved.
- Under the Galleries section, click on Web Parts.
- Click on the New button.
- Check the checkbox for the calendar web part just created.
- Click on Populate Gallery.
- Add a new Permission Level for this particular type of web part, as shown above, and grant it basic rights plus the Add right, as without Add, it won't work.
- The web part is now ready for use on this site.
Sample, entire Web part class code file:
"); sbHeader.AppendLine("DotNetFun PROGRAMMING"); sbHeader.AppendLine(" |
---|
");
sbLegend.AppendLine("Legend: "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" "); sbLegend.AppendLine(" | ");
sbLegend.AppendLine("");
sbLegend.AppendLine("Options: "); if (blnCanAddEvents) sbLegend.AppendLine(String.Format(" |
An error occurred: " + // Error.Message + " " + (Error.StackTrace != null ? Error.StackTrace : "") + // "
"); }); } protected override void OnInit(EventArgs e) { base.OnInit(e); } private enum ViewType { Day, Week, Month, Timeline } protected override void CreateChildControls() { try { SPSecurity.RunWithElevatedPrivileges( delegate() { base.CreateChildControls(); ViewType viewType = ViewType.Month; using (SPWeb web = SPContext.Current.Site.OpenWeb(this.webGuid)) { // Change Cal-view based on if QueryString specifies it: if (Page.Request.QueryString["CalendarPeriod"] != null) { switch (Page.Request.QueryString["CalendarPeriod"].ToString().ToLower()) { case "day": this.calendarView.ViewType = "day"; viewType = ViewType.Day; break; case "week": this.calendarView.ViewType = "week"; viewType = ViewType.Week; break; case "timeline": this.calendarView.ViewType = "timeline"; viewType = ViewType.Timeline; break; default: this.calendarView.ViewType = "month"; viewType = ViewType.Month; break; } } SPList listDotNetFunCalendar = web.Lists[this.strListName]; if (listDotNetFunCalendar.Views["Calendar"] == null) throw new ApplicationException("The Calendar view was not found. Please create one accordingly."); SPView viewDotNetFunCalendar = listDotNetFunCalendar.Views["Calendar"]; if (this.calendarView.ViewGuid != null) viewDotNetFunCalendar = listDotNetFunCalendar.Views[new Guid(this.calendarView.ViewGuid)]; this.listGuid = listDotNetFunCalendar.ID; DateTime dtStart = DateTime.Now; if (this.calendarView.SelectedDate != null) dtStart = DateTime.Parse(this.calendarView.SelectedDate).AddDays(-7); DateTime dtEnd = dtStart.AddMonths(1).AddDays(7); SPQuery query = new SPQuery(); query.Query = String.Format( "", viewDotNetFunCalendar.Query)); SPCalendarItemCollection items = new SPCalendarItemCollection(); foreach (SPListItem listItem in listDotNetFunCalendar.GetItems(viewDotNetFunCalendar)) { SPCalendarItem calItem = new SPCalendarItem(); calItem.ItemID = listItem["ID"].ToString(); calItem.Title = listItem["Title"].ToString(); calItem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian); calItem.StartDate = (DateTime)listItem["Start Time"]; calItem.ItemID = listItem.ID.ToString(); calItem.WorkSpaceLink = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); // Only allow color-coding, which requires all all-day // event, if a Month view: if (viewType == ViewType.Month) calItem.IsAllDayEvent = true; calItem.DisplayFormUrl = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); if (listItem["End Time"] != null) { calItem.hasEndDate = true; calItem.EndDate = (DateTime)listItem["End Time"]; } else calItem.hasEndDate = false; if (listItem["Description"] != null) calItem.Description = listItem["Description"].ToString(); if (listItem["Location"] != null) calItem.Location = listItem["Location"].ToString(); // Define CSS based on category: // Note, CSS styles are stored in CORE.CSS, here: // %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\STYLES // Append "sel" (without quotes) to the CSS Class name to // override the onmouseover CSS changes in the CORE.CSS file. switch (listItem["Category"].ToString()) { case "Programming/Scheduling": calItem.BackgroundColorClassName = "DotNetFunCalendarProgramming"; break; case "Production": calItem.BackgroundColorClassName = "DotNetFunCalendarProduction"; break; case "Ad Sales": calItem.BackgroundColorClassName = "DotNetFunCalendarAdSales"; break; case "Marketing/Promotions": calItem.BackgroundColorClassName = "DotNetFunCalendarMarketing"; break; case "PR": calItem.BackgroundColorClassName = "DotNetFunCalendarPR"; break; case "Online/Interactive": calItem.BackgroundColorClassName = "DotNetFunCalendarInteractive"; break; case "Company Events/HR": calItem.BackgroundColorClassName = "DotNetFunCalendarHR"; break; case "General": calItem.BackgroundColorClassName = "DotNetFunCalendarGeneral"; break; } items.Add(calItem); } // Set calendar-only properties: this.calendarView.DisplayItemFormUrl = String.Format("/Lists/{0}/DispForm.aspx", this.strListName); this.calendarView.EditItemFormUrl = String.Format("/Lists/{0}/EditForm.aspx", this.strListName); this.calendarView.NewItemFormUrl = String.Format("/Lists/{0}/NewForm.aspx", this.strListName); this.calendarView.EnableViewState = true; this.calendarView.DataSource = items; this.calendarView.DataBind(); this.DataBind(); } this.Controls.Add(this.calendarView); }); } catch (Exception err) { this.HandleException(err); } } } }
4 comments:
I don't know if you had this problem or not.. But BackgroundColorClassName only works if you have IsAllDayEvent set to true. If set to false, it will not change at all.
How do I add the Full toolbar to the SPCalendarView?
Thanks,
Alberto da Silva
I am using this code but it is not displaying any event on the calebdar. Please Help me
I like your blog application.This is one of the Important post.I like your blog clarity.This is one of the nice post.
Post a Comment