SPCalendarView not working as expected in SharePoint Foundation 2010

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.

5 Replies to “SPCalendarView not working as expected in SharePoint Foundation 2010”

  1. Well in the monthly view you only get the SPCalendarItem.Title displayed. In the weekly and daily views, it will display whatever text you put into the SPCalendarItem.Description property in addition to the Title.

  2. Can you please tell me when you would use a Content Editor Web Part vs. the new CMS Field Controls that allow the same type of fuinlconatity? I am trying to convince a colleague that Page Layouts and CMS Field controls should be used rather then having static content editor web parts everywhere.

  3. In case anyone happens to still be working with SPCalendarViews in SharePoint 2010, the SPCalendarView control will work as expected if you place it within a web part that inherits from Microsoft.SharePoint.WebPartPages.WebPart rather than from System.Web.UI.WebControls.WebParts.WebPart. If you use the SharePoint namespace, you can display it with V4 rendering and can bind directly to a SPCalendarItemCollection.
    See http://social.msdn.microsoft.com/Forums/en-US/f10155d3-8ac6-4d8c-8c33-b4d5dbae6e2d/spcalendarview-in-sharepoint-2010-placed-on-custom-aspx-form-does-not-respond-to-any-actions?forum=sharepointdevelopmentprevious

Leave a Reply to Pete Cancel reply

Your email address will not be published. Required fields are marked *

*

*