Create View on Discussion List by Powershell

17/02/2016 09:34

I tried to create view by standard way, but the view still didn't show my columns...Following workaround copies Management view in which fields are visible


Add-PSSnapin microsoft.sharepoint.powershell -ea 0
$ErrorActionPreference = "Stop"
$webUrl = "https://yourcommunitysite"
$viewUrl = "/Lists/Community%20Discussion"

$web = get-spweb $webUrl

$listUrl = $web.ServerRelativeUrl + $viewUrl
$list = $web.GetList($listUrl);
$list.Title

$view = $list.Views["Management"];


#Remove my test views
$views = $list.Views ' Where-Object {$_.Url -match "testView"}

foreach($v in $views)
{
    $list.Views.Delete($v.ID);
}

$list.Update();


#Create copy of Management view and make update
$newview = $view.Clone("TestsView0", 20, $true, $false);
$newview.Query = "";
$newview.ViewFields.Add("yourFieldName") #Add fields
$newview.Update();