Calendar view is missing in View selector

10/06/2016 15:50

If we create SharePoint list calendar view programmatically, there is an issue, this view disappears from view selector in list web part. There is manual step needed to enable/disable View selector in web part properties. Here is a script to do it programmatically by PowerShell (code in C# is similar).


Add-PSSnapin microsoft.sharepoint.powershell -ea 0

$assembly=[void][reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$webUrl = "https://yoursitecollectionurl"
$listName = "Your list with Calendar view"

$web = Get-SPWeb $webUrl

$list = $web.Lists[$listName]

#select all views other than calendar
$views = $list.Views ' where {$_.Title -notmatch "Calendar"} ' select Title, Url
$views

foreach ($view in $views)
{
    $pageUrl = $webUrl + "/" + $view.Url
    $pageUrl
    $webpartmanager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

 #see list of web parts on the page

  $wp = $webpartmanager.WebParts[0];

  $wp.ViewSelectorFetchAsync = $true;
  $webpartmanager.SaveChanges($wp);

  $wp.ViewSelectorFetchAsync = $true;
  $webpartmanager.SaveChanges($wp);

  "Setting View selector on page '{0}'" -f $pageUrl
}