In this post, I will show you how to create a view for each Document Library or List in a Site Collection or sub-sites of a Web using PowerShell.
1.) Using SharePoint, create a view on a list and select the columns* you want, so that we can grab the view with PowerShell and send it everywhere.
*Make sure that you select columns that are available everywhere that you are wanting to apply this view, else the view will fail to create.
2.) Open SharePoint Management Shell and grab the new view
$Web = Get-SPWeb http://SiteCollection/Web $List = $Web.GetList("/Web/List") $NewView = $list.Views["ViewName from Step 1"]
To display the view and its properties:
$NewView
Take note of the ViewFields, these are the columns you selected in step 1.
3.) Create the view on all Document Libraries in a Site Collection
$viewDefaultView = $False $Site = Get-SPSite http://SiteCollection/ $Webs = $Site.AllWebs foreach($Web in $Webs) { $Weblists = $Web.Lists for ($i = 0; $i -lt $WebLists.Count; $i++) { $list = $web.Lists[$i]; $webUrl = $web.Url try { if($list.BaseTemplate -eq "DocumentLibrary"){ $CreatedView = $list.Views.Add("QuickEdit View", $newview.viewFields, $NewView.Query, $NewView.RowLimit, $NewView.Paged, $viewDefaultView) Write-Host ("View '" + $CreatedView.Title + "' created in list '" + $list.Title + "' on site " + $webUrl) } } catch { Write-Host ("There was a problem trying to create the view in the site " + $webUrl + ": " + $web) } } }
Notes:
– To apply the view to all lists, not just Document Libraries, remove the IF Statement wrapper.
– To create the view in all Libraries in the SubSites of a Site rather than the Site Collection replace the $Site and $Webs with:
$Site = Get-SPWeb http://SiteCollection/Web/ $Webs = $site.webs
– To delete a view in all Libraries by view name, place the following in place of the code within the IF Statement:
$ViewToDEL = $list.Views["View Name"] $list.Views.Delete($ViewToDEL.ID)
– To create a view manually in PowerShell check out this blog post
I needed to create a view in all Document Libraries because I did not have a view that did not use Group By, which meant the Quick Edit was always Greyed Out.