Programmatically created calendar view does not appear on view selector menu RRS feed

  • Question

  • Hi!

    I'm using the following code to create a calendar view for a list:

    using (var site = new SPSite("http://localhost"))
        var viewFields = new System.Collections.Specialized.StringCollection { dateStartFieldName, dateEndFieldName, titleFieldName };
        var query = string.Format("<Where><DateRangesOverlap><FieldRef Name='{0}' /><FieldRef Name='{1}' /><Value Type='DateTime'><Month /></Value></DateRangesOverlap></Where>", dateStartFieldName, dateEndFieldName);
        var viewData = string.Format("<FieldRef Name='{0}' Type='CalendarMonthTitle' /><FieldRef Name='{0}' Type='CalendarWeekTitle' /><FieldRef Name='' Type='CalendarWeekLocation' /><FieldRef Name='{0}' Type='CalendarDayTitle' /><FieldRef Name='' Type='CalendarDayLocation' />", titleFieldName);
        var list = site.RootWeb.Lists["ESM Leaves"];
        var newView = list.Views.Add("Calendar11", viewFields, query, 0, true, false, SPViewCollection.SPViewType.Calendar, false);
        newView.ViewData = viewData;
        newView.MobileView = true;

    My problem is that even though the view is created, it does not appear in the View links just above my list. It only appears in the drop down menu of views in the ribbon. On the contrary, if I create the view using the browser user interface (not programmatically), the view appears in both places.

    Do you have any idea why this might be happening?

    Dimitris Papadimitriou, Software Development Professional

    • Edited by papadi Wednesday, March 26, 2014 11:18 AM More appropriate title
    Monday, March 24, 2014 2:10 PM


  • Hi papadi,

    I can reproduce the issue that creating calendar view using programming method, the view isn’t shown in view selector menu, and this only happens to calendar view, html view or other views are shown in the menu.

    After editing the web part, disable the selector menu, then re-enable the selector menu again, I can see the calendar view shown in the view selector menu, create a new calendar view using programming, the calendar view shows as expected.

    This seems indicate there is some issue in the view selector menu display, I would suggest you to first disable the view selector menu through edit the web part properties, then create the calendar view.



    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Qiao Wei
    TechNet Community Support

    • Marked as answer by papadi Wednesday, March 26, 2014 9:18 AM
    Wednesday, March 26, 2014 5:38 AM

All replies