入门请看KaneBoy写很好的入门的ppt----Walkthrough-WebPart 。
第一个webpart 是Event Calendar 读取Event,比较简单,重写Calendar控件就可以了,我参考了CodeProject的一位前辈 Custom_Calendar_Web_Part,不过那个的取时间有点问题,自己笨笨的写了一个
Code
//没贴CSS上来 将注释去掉就行了
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.ComponentModel;
namespace SharePoint.Panda.Calendar {
[Guid ( "9c7c129b-9442-4d16-a30f-c1d43c35c0e3" )]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart {
public WebPart1 () {
}
System.Web.UI.WebControls.Calendar cal;
string url;
Literal redire = new Literal ();
private string _eventName = "CLP.SRS.Events"; //list name
[Personalizable ( PersonalizationScope.Shared ), WebBrowsable ( true ), WebDisplayName ( "Event name" ), WebDescription ( "Input event name" )]
public string eventName {
get { return _eventName; }
set { _eventName = value; }
}
protected override void OnInit ( EventArgs e ) {
this.EnsureChildControls ();
this.Load += new EventHandler (Calendar_Load);
base.OnInit ( e );
}
void Calendar_Load ( object sender, EventArgs e ) {
if ( string.IsNullOrEmpty ( Page.Request.Url.ToString() ) ) {
return;
}
if ( this.WebPartManager.DisplayMode == WebPartManager.EditDisplayMode ) {
return;
}
}
protected override void CreateChildControls () {
cal = new System.Web.UI.WebControls.Calendar ();
this.cal.ID = "Calendar";
//this.cal.BorderColor = System.Drawing.ColorTranslator.FromHtml ( "#f1f9e2" );
//this.cal.BorderStyle = BorderStyle.Solid;
//this.cal.BorderColor = System.Drawing.ColorTranslator.FromHtml ( "#f1f9e2" );
//this.cal.BorderWidth = 0;
//this.cal.SelectorStyle.Font.Bold = true;
this.cal.CssClass = "CLPSRS_Calendar";
this.cal.TitleStyle.CssClass = "Calenader_Header";
//this.cal.DayHeaderStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml ( "#818080" );
//this.cal.OtherMonthDayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml ( "#f1f9e2" );
//this.cal.OtherMonthDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml ( "#f1f9e2" );
//this.cal.OtherMonthDayStyle.Font.Bold = false;
this.cal.OtherMonthDayStyle.CssClass = "Calendar_IsOtherMonth";
//this.cal.SelectedDayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml ( "#818080" );
//this.cal.SelectedDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml ( "#ffffff" );
this.cal.SelectedDayStyle.CssClass = "Calendar_SelectedDay";
this.cal.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler ( OnDayRender );
this.cal.SelectionChanged += new EventHandler ( this.cal_SelectionChanged );
this.Controls.Add ( cal );
this.Controls.Add ( redire );
}
protected void cal_SelectionChanged ( object sender, EventArgs e ) {
System.Web.UI.WebControls.Calendar sendCal = ( System.Web.UI.WebControls.Calendar ) sender;
RenderEventsByDate ( sendCal.SelectedDate );
}
private void RenderEventsByDate ( DateTime selectedDate ) {
if ( !string.IsNullOrEmpty ( eventName ) ) {
SPSecurity.RunWithElevatedPrivileges ( delegate () {
using ( SPSite site = new SPSite ( SPContext.Current.Site.ID ) ) {
try {
using ( SPWeb web = site.RootWeb ) {
try {
SPList calendarList = web.Lists [eventName];
SPQuery query = new SPQuery ();
query.Query = string.Format ( "<Where><Eq><FieldRef Name='EventDate'/><Value Type='DateTime'>{0}</Value></Eq></Where>", SPUtility.CreateISO8601DateTimeFromSystemDateTime ( selectedDate ) );
SPListItemCollection calendarItems = calendarList.GetItems ( query );
url = SPContext.Current.Web.Url + "/Lists/" + eventName.Replace ( ".", "" ) + "/calendar.aspx?CalendarDate=" +
selectedDate.Month.ToString () + "/" + selectedDate.Day.ToString () + "/" +
selectedDate.Year.ToString () + "&CalendarPeriod=month";
redire.Text = "<script language='javascript'>window.location='" + url + "'</script>";
} catch ( Exception ee ) {
web.Dispose ();
string error = ee.ToString ();
}
}
} catch ( Exception ee ) {
site.Dispose ();
string error = ee.ToString ();
}
}
} );
}
}
protected void OnDayRender ( object sender, DayRenderEventArgs e ) {
CalendarDay day = ( ( DayRenderEventArgs ) e ).Day;
TableCell cell = ( ( DayRenderEventArgs ) e ).Cell;
cell.CssClass = "Calendar_Day";
//cell.Style.Add ( "color", "#818080" );
if ( day.IsToday && !day.IsOtherMonth ) {
// cell.Font.Bold = true;
//cell.BackColor = System.Drawing.ColorTranslator.FromHtml ( "#6f8c16" );
cell.CssClass = "Calendar_IsToday";
} else if ( day.IsOtherMonth ) {
//cell.Style.Add ( "color", "#f1f9e2" );
cell.CssClass = "Calendar_IsOtherMonth";
cell.Text = " ";
}
if (!day.IsOtherMonth ){
if ( !string.IsNullOrEmpty ( eventName ) ) {
SPSecurity.RunWithElevatedPrivileges ( delegate () {
using ( SPSite site = new SPSite ( SPContext.Current.Site.ID ) ) {
try {
using ( SPWeb web = site.RootWeb ) {
try {
SPQuery query = new SPQuery ();
query.Query = string.Format ( "<Where><Eq><FieldRef Name='EventDate'/><Value Type='DateTime'>{0}</Value></Eq></Where>", SPUtility.CreateISO8601DateTimeFromSystemDateTime ( day.Date ) );
SPListItemCollection calendarItems = web.Lists [eventName].GetItems ( query );
if ( calendarItems.Count > 0 ) {
//cell.BackColor = System.Drawing.Color.Khaki;
cell.CssClass = "Calendar_EventDay";
}
} catch ( Exception ee ) {
web.Dispose ();
string error = ee.ToString ();
}
}
} catch ( Exception ee ) {
site.Dispose ();
string error = ee.ToString ();
}
}
} );
}
}
}
protected override void Render ( HtmlTextWriter writer ) {
EnsureChildControls ();
redire.RenderControl ( writer );
cal.RenderControl ( writer );
}
}
}
然后部署Webpart..... 就不说了