In the new version of the Content By Type webpart, I added the option to rollup items into a calendar. In a later post I will describe how you can do that using the webpart. In this post I will show you how I did it in the code. When you start it all seems very easy, but there were a few things that caused me some headaches. I hope you don’t run into them as well after reading this item.
Step 1 – Setting up the controls
I implemented all the calendar specific code in a special control:
In the CreateChildControls of my webpart, I just add this control to the collection:
In the CreateChildControls of my SharePointCalendar class, I setup the SPCalendarView and add it to the controls collection:
You can find SPCalendarView in the Microsoft.SharePoint.WebControls namespace.
Step 2 – Adding data and testing
The data that will be displayed in the calendar is loaded in GetCalendarItems. In this case it is dummy data, but you will get the idea:
If you now build your webpart and add it to a page, it should look like this:
Step 3 – The ViewType
Looks nice and it indeed it very simple to do. But now you click one of the links “Week” or “Day” to switch to the daily or weekly view. That doesn’t work. The webpart simply does what it does and it shows you the monthly view. If you look at the url however, you will see that the calendar view added a querystring parameter called “CalendarPeriod“.
Add the following snippet to the CreateChildControls of the SharePointCalendar to make it work:
The documentation for the ViewType property shows you the possible values, but please notice that you need to set them in lower case!. Not sure what the option “timeline” does. It looks like a normal monthly view if you use it.
Step 4 – The DisplayFormUrl problem
The property DisplayFormUrl expects are relative url to the item that you added to the calendar. In the sample above, I added a url to the News site. If you look at the properties of the hyperlink, you will see this link:
The calendar view automatically adds a querystring parameter ID to your urls. In this case this is not a problem. In the Content By Type webpart however, I add the same links that are generated by the out of the box ContentQuery webpart. These urls look like this:
When you add “?ID=” to that url (that is what the calendar view does), your link will not work and the page will display an “Unknown error“:
In that tstredirect page, I decode the querystring GoTo parameter and redirect to that url.
I am not very happy with that last approach, there should be a better way. If you have a suggestion, please let me know. So after all, it still is pretty easy to use the SPCalendarView in your webparts, if you are aware of these things. In the next post I will show you how I exactly implemented it in the Content By Type webpart.