Banner Advert
Yaroslav Pentsarskyy
Posted on February 12, 2013 by
Leave a comment

You probably have seen many times how SharePoint renders it’s calendar view.


moth view


One of the other features that SharePoint calendar has it’s the ability to set which fields do you want to display in more detailed (week and day) view. In the example below you can see how CreatedBy field of the item is showing with the title of the item:


day view


To chose which fields are going to render in a week and day view, navigate to the view settings or create a new calendar view on the list. In here you will be able to define Calendar Columns as well as filter options.


calendar columns


calendar filter


But how do you achieve the same programmatically, when provisioning a view in code. Here it is:



System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();

viewFields.Add(“Title”);

SPView view = oViewCollection.Add(“NewCalendarView”, viewFields, string.Empty, 100, true, false, SPViewCollection.SPViewType.Calendar, false);
view.ViewData = @”<FieldRef Name=”"Title”" Type=”"CalendarMonthTitle”"/>
<FieldRef Name=”"Title”" Type=”"CalendarWeekTitle”"/>
<FieldRef Name=”"Created”" Type=”"CalendarWeekLocation”"/>
<FieldRef Name=”"Title”" Type=”"CalendarDayTitle”"/>
<FieldRef Name=”"Created”" Type=”"CalendarDayLocation”"/>”;

view.DefaultView = true;
view.Update();


Above we assume that oViewCollection is a reference to a view collection on SPWeb. We’re also adding only one field viewFields.Add(“Title”); to a view, where in fact you will probably add more fields.
Finally, the ViewData will describe view column, in our case it’s Title and Created fields displayed in a calendar view details. By the way, more on the parameters ofSPViewCollection.Add() here.


You can also define the query for the view which we didn’t specify, so the query will display all the items with no filter.

Enjoy!

PS: More development tips and tricks, especially on SharePoint 2013 is in my new book here.


This article was originally posted here.

No comments yet, why not be the first?

Add a comment