Add Calendar List View WebPart to SharePoint page

Follow up from my previous article to Add List View WebPart to SharePoint Page, I spent a bit of time figuring out why setting Calendar view to the list view webpart is not working.

My custom list have two views (default list view & calendar view) and I would like to display the Calendar view when added to SharePoint page. After spending sometime trying to figure out, I finally find the solution. Hopefully it save you a lot of time. 

Solution

Change XsltListViewWebPart into ListViewWebPart. What the …. 

private void AddCalendarViewWebPart(SPFile file, SPList list, string webPartZone, int webPartOrder, Guid viewId)
{
   SPLimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
   ListViewWebPart wp = null;
   list.ParentWeb.AllowUnsafeUpdates = true;

   try
   {
       // create an instance of the ListViewWebPart
       wp = new ListViewWebPart();

       // convert the list GUID to a string, must include braces (ToString("B")) and be in uppers (ToUpper())
       wp.ListName = list.ID.ToString("B").ToUpper();

       // optionally set the title of the web part
       wp.Title = list.Title;
       wp.ViewGuid = viewId.ToString("B").ToUpper(); 
       wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

       // add the web part to the limited web part manager.  
       limitedWebPartManager.AddWebPart(wp, webPartZone, webPartOrder);

       // you need to update the list because a view was just added to it
       list.Update();
   }
   finally
   {
       // do some fun cleanup of disposable items.  if you are wondering about the .Web.Dispose() bit look for my
       // blog article on the memory leaks in the SPLimitedWebPartManager.
       if (limitedWebPartManager != null)
       {
           if (limitedWebPartManager.Web != null)
           {
              limitedWebPartManager.Web.Dispose();
           }

           limitedWebPartManager.Dispose();
       }

       if (wp != null)
       {
           wp.Dispose();
       }
       list.ParentWeb.AllowUnsafeUpdates = true;
   }
}
Advertisements

Tagged: , , , , , ,

4 thoughts on “Add Calendar List View WebPart to SharePoint page

  1. […] If you are planning to set Calendar view for the list, this will not work. Take a look on my other article to Add Calendar List View WebPart to Page. […]

  2. Ashok Kumar Sah 06/23/2016 at 6:59 AM Reply

    can we do this by the help of powershell with csom

  3. Bernado 02/19/2017 at 8:39 PM Reply

    Thanks! This saved me quite a bit of time!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: