|
I'm currently working to make sure all of my custom code works correctly in SharePoint Foundation 2010. One of my webparts utilized the SPCalendarView and I would set the datasource to be a SPCalendarItemCollection and it would render a calendar with the events I specified in the collection. Well, this no longer works in SharePoint Foundation 2010. I was able to achieve a workaround with a little help from reflector. You'll need to create two classes:
public class MySPCalendarDataSource : Control, System.Web.UI.IDataSource
{
private MyCalendarSourceView _view;
private static readonly object EventDataSourceChanged = new object();
private SPCalendarItemCollection _data = null;
public MySPCalendarDataSource(SPCalendarItemCollection data)
: base()
{
_data = data;
}
event EventHandler System.Web.UI.IDataSource.DataSourceChanged
{
add
{
base.Events.AddHandler(EventDataSourceChanged, value);
}
remove
{
base.Events.RemoveHandler(EventDataSourceChanged, value);
}
}
protected DataSourceView GetView(string viewName)
{
return this.internalGetView(viewName);
}
protected ICollection GetViewNames()
{
return new string[] { this.internalGetView(null).Name };
}
private MyCalendarSourceView internalGetView(string viewName)
{
if (this._view == null)
{
this._view = new MyCalendarSourceView(this, viewName, _data);
}
return this._view;
}
private void OnDataSourceChanged(EventArgs e)
{
EventHandler handler = (EventHandler)base.Events[EventDataSourceChanged];
if (handler != null)
{
handler(this, e);
}
}
protected virtual void RaiseDataSourceChangedEvent(EventArgs e)
{
this.OnDataSourceChanged(e);
}
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
{
return this.GetView(viewName);
}
ICollection System.Web.UI.IDataSource.GetViewNames()
{
return this.GetViewNames();
}
}
public class MyCalendarSourceView : DataSourceView
{
SPCalendarItemCollection _data;
public MyCalendarSourceView(System.Web.UI.IDataSource ds, string viewName, SPCalendarItemCollection data) : base(ds, viewName)
{
_data = data;
}
protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
{
return _data;
}
}
And where in SharePoint 2007 you could do this:
SPCalendarItemCollection items = GetCalendarItems();
SPCalendarView calView = new SPCalendarView();
calView.DataSource = items;
calView.DataBind();
You now have to do this:
SPCalendarItemCollection items = GetCalendarItems();
SPCalendarView calView = new SPCalendarView();
calView.EnableV4Rendering = false;
calView.DataSource = new MySPCalendarDataSource(items);
calView.DataBind();
Also, please note that for this to work, we do need to disable the V4 ajax rendering.
Powered by AkoComment! |