none
Can not create calendar view in custom list programatically using CSOM in Sharepoint Online RRS feed

  • Question

  • I am not able to create calendar view in custom list programatically using CSOM in Sharepoint online. I get Server exception "Cannot complete this action. Please try again". I am able to do the same using UI but not through code. I used the following code:

                Web web = context.Web;
                context.Load(web);
                context.ExecuteQuery();
                List list = context.Web.Lists.GetByTitle("myCal");
                context.Load(list);
                context.ExecuteQuery();
                ViewCreationInformation vci = new ViewCreationInformation();
                vci.Title = "Calviews1";
                vci.ViewTypeKind = ViewType.Calendar;
                vci.RowLimit = 0;            
                View view = list.Views.Add(vci);
                context.ExecuteQuery();

    If i use the code to create any other type of view in custom List, it works. Also if I create Calendar View in a Calendar list it still works. But not just if I use the code to create Calendar view for Custom List

    Also the code works if I try to create the Calendar View in custom list in SharePoint 2013 and 2016 on Premise but not in SharePoint Online


    Thursday, November 24, 2016 7:13 AM

All replies

  • Hi,

    Here is the test code for your reference:

    string viewTitle = "Calendar View";
                    string[] viewFields = new string[] { "Created", "Modified", "Title" };
                    string viewQuery = "<Where><DateRangesOverlap><FieldRef Name=\"Created\" /><FieldRef Name=\"Modified\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where>";
                    
                    ViewCreationInformation newCalendarViewCI = new ViewCreationInformation
                    {
                        Title = viewTitle,
                        ViewTypeKind = ViewType.Calendar | ViewType.Recurrence,
                        ViewFields = viewFields,
                        SetAsDefaultView = true,
                        PersonalView = false,
                        RowLimit = 0,
                        Paged = false,
                        Query = viewQuery
                    };
    
                    View view = list.Views.Add(newCalendarViewCI);
                    context.Load(view);
                    context.ExecuteQuery();

    (As custom list has different fields, you could try to update the fields so make the calendar view meaningful, or you could create one and monitor it's ListViewXml)

    Here is the referenced thread:

    https://danonnet.wordpress.com/2016/03/15/how-to-create-a-calendar-view-to-show-recurrence-events-in-sharepoint-online-with-csom/

    Best Regards,

    Lee


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

    Tuesday, November 29, 2016 7:25 AM
  • Thanks. It works :)
    Monday, December 12, 2016 5:22 AM
  • Hi,

    You could mark the replies as answers if the replies helped you, it also may help others who has similar issue.

    Thanks for your cooperation.

    Best Regards,

    Lee


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

    Monday, December 12, 2016 5:34 AM