Thursday, October 9, 2008

Custom Calendar for MOSS 2007

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace CalendarWebPart
{
///
/// Description for CalControl.
///

[DefaultProperty("Text"),
ToolboxData("<{0}:CalControl runat=server>"),
XmlRoot(Namespace = "CalControl")]
public class CalControl : Microsoft.SharePoint.WebPartPages.WebPart
{
Calendar NewCalendar = new Calendar();
Label lblMessage = new Label();
//string strCommonUrl = null;
//bool CommonFlag = false;
HyperLink lbl;
private const string defaultText = "";
private string _text = string.Empty;
private string _col = string.Empty;
public CalControl()
{
try
{
specifyColors();
this.Controls.Add(NewCalendar);
this.Controls.Add(lblMessage);
this.NewCalendar.SelectionChanged += new EventHandler(NewCalendar_SelectionChanged);

}
catch (Exception Ex)
{
lblMessage.Text = "Problems while loading the page. " + Ex.Message.ToString();
}
}

protected void specifyColors()
{
NewCalendar.BackColor = System.Drawing.Color.FromName("#f9f9f9");

NewCalendar.ForeColor = System.Drawing.Color.FromName("#444444");

NewCalendar.TitleStyle.BackColor = System.Drawing.Color.White;

NewCalendar.NextPrevStyle.BackColor = System.Drawing.Color.FromName("#f9f9f9");

NewCalendar.TitleStyle.ForeColor = System.Drawing.Color.FromName("#252525");

NewCalendar.TitleStyle.Font.Bold = true;

NewCalendar.DayHeaderStyle.BackColor = System.Drawing.Color.FromName("#fdd127");

NewCalendar.DayHeaderStyle.ForeColor = System.Drawing.Color.FromName("#08204a");

NewCalendar.TodayDayStyle.BackColor = System.Drawing.Color.FromName("#c9ccd1");

NewCalendar.NextPrevStyle.ForeColor = System.Drawing.Color.FromName("#252525");
NewCalendar.NextPrevStyle.Font.Bold = true;

NewCalendar.Height = new Unit(150);
NewCalendar.Width = new Unit(216);

NewCalendar.OtherMonthDayStyle.ForeColor = System.Drawing.Color.FromName("#d3d3d4");

NewCalendar.SelectedDayStyle.ForeColor = System.Drawing.Color.FromName("#ff3000");
NewCalendar.SelectorStyle.BackColor = System.Drawing.Color.FromArgb(255, 204, 102);

NewCalendar.ShowGridLines = false;

NewCalendar.BorderColor = System.Drawing.Color.FromName("#d6d6d6");
NewCalendar.WeekendDayStyle.ForeColor = System.Drawing.Color.FromName("#ff3000");
//NewCalendar.OtherMonthDayStyle.ForeColor = System.Drawing.Color.FromArgb(231, 240, 253);

}

private string text = defaultText;

[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Shared),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

[Browsable(true),
Category("Event"),
DefaultValue(defaultText),
WebPartStorage(Storage.Shared),
FriendlyName("Event"),
Description("Event")]
[XmlElement(ElementName = "Event")]
public string Event
{
get
{
return _text;
}

set
{
_text = value;
}
}

[Browsable(true),
Category("Event"),
DefaultValue(defaultText),
WebPartStorage(Storage.Shared),
FriendlyName("Column"),
Description("Column")]
[XmlElement(ElementName = "Column")]
public string Column
{
get
{
return _col;
}

set
{
_col = value;
}
}

///
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
///

///An array of references to ToolPart objects.
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// return toolparts;
// }

///
/// Render this Web Part to the output parameter specified.
///

/// The HTML writer to write out to
protected override void RenderWebPart(HtmlTextWriter output)
{
try
{
SPWeb spwCurr = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
output.Write(SPEncode.HtmlEncode(Text));

if (this.Event.ToString().TrimStart().TrimEnd().Equals(""))
{
this.lblMessage.Text = "Please provide the Events name in Properties";
}

/// Table Starts
output.Write("");

output.Write("");

//output.Write("");
// output.Write("");
//output.Write("");

//output.Write("");
// output.Write("");
//output.Write("");

output.Write("");
output.Write("");
output.Write("");

output.Write("");
output.Write("");
output.Write("");



output.Write("
");
NewCalendar.RenderControl(output);
output.Write("
");
// output.Write("Birthday");
// output.Write("
");
// output.Write("Holiday");
// output.Write("
");
output.Write("");
output.Write("
");
lblMessage.RenderControl(output);
output.Write("
");

}
catch (Exception Ex)
{
lblMessage.Text = "Error while rendering the web part. " + Ex.Message.ToString();
}
}

protected override void CreateChildControls()
{
try
{
NewCalendar.DayRender += new DayRenderEventHandler(NewCalendar_DayRender);
}
catch (Exception Ex)
{
this.lblMessage.Text = "Problems while Creating child Controls. " + Ex.Message.ToString();
}
}

private void NewCalendar_SelectionChanged(object sender, EventArgs e)
{
if (NewCalendar.SelectedDates.Count > 0)
{
// this.Page.Response.Redirect(lbl.NavigateUrl.ToString());

}
}
private void NewCalendar_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
try
{
e.Day.IsSelectable = false;
if (!(e.Day.IsOtherMonth))
{
SPWeb spwCurr = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
Microsoft.SharePoint.SPList list;


try
{
list = spwCurr.Lists[this.Event.ToString().TrimStart().TrimEnd()];
}
catch
{
throw new Exception("Please specify a valid Events Name in the WebPart Properties");
}

string strEventInternal = list.DefaultViewUrl.ToString();
strEventInternal = strEventInternal.Remove(strEventInternal.Length - 14, 14);
strEventInternal = strEventInternal.Remove(0, strEventInternal.LastIndexOf("/") + 1);

DateTime start;
string strFUrl = spwCurr.Url.ToString() + "/Lists/" + strEventInternal + "/DispForm.aspx?ID=";
string strID = "0";
string strRUrl = "&Source=" + spwCurr.Url.ToString() + "/Lists/" + strEventInternal + "/AllItems.aspx";

string strUrlF = spwCurr.Url.ToString() + "/Lists/" + strEventInternal + "/calendar.aspx?CalendarDate=";
string strDate = "0";
string strUrlR = "&View={4FF74E9E-B137-4E79-8DA7-AACF7D9CC7C4}&CalendarPeriod=day";

lbl = new HyperLink();
lbl.Text = "*";
lbl.Font.Bold = true;

foreach (Microsoft.SharePoint.SPListItem item in list.Items)
{
try
{
start = DateTime.Parse(item[this.Column.ToString()].ToString());
}
catch (Exception Ex)
{
//throw new Exception("Please specify a valid column name (start/end) date");
throw new Exception(Ex.Message.ToString());
}

if (start.ToShortDateString() == e.Day.Date.ToShortDateString())
{
strID = item.ID.ToString();
e.Cell.ToolTip += "\n" + "* " + item["LinkTitle"].ToString();

string dat = start.ToShortDateString().ToString();
string year = start.Date.Year.ToString();
string month = start.Date.Month.ToString();
string date = start.Date.Day.ToString();

strDate = year.ToString() + "-" + month.ToString() + "-" + date.ToString();

e.Cell.BackColor = System.Drawing.Color.FromName("#ff9e18");//AAD423

try
{
// if(item["Birthday"].ToString().Equals("True"))
// {
// e.Cell.BackColor = System.Drawing.Color.FromArgb(255, 210, 250);
// }
// if(item["Holiday"].ToString().Equals("True"))
// {
// e.Cell.BackColor = System.Drawing.Color.FromArgb(255, 188, 188);
// }

if (e.Cell.Controls.Count == 1)
{
e.Day.IsSelectable = true;
lbl.NavigateUrl = strFUrl + strID + strRUrl;
e.Cell.Controls.Add(lbl);
}
else if (e.Cell.Controls.Count > 1)
{
e.Day.IsSelectable = true;
lbl.NavigateUrl = strUrlF + strDate + strUrlR;
}
}
catch
{
throw new Exception("Birthday, Holiday field is missing. Please insert the same");
}
}
}
}
}
catch (Exception Ex)
{
lblMessage.Text = Ex.Message.ToString();
}
}
}
}

No comments: