Sorting SharePoint Views Alphabetically

Out of the Box, SharePoint 2010 does not provide any mechanism for sorting through your views.  As a list/library grows and different views are created, this often means that you are left with an assortment of random and poorly organised views that look pretty bad.

You can use the following PowerShell Script to sort the views.

You will need to replace the values in $webURL and $listName to match your environment, with these values being the URL of the site and the name of the list/library that you want to sort on.  It works by copying all the views into a sorted list, then deleting them and rebuilding them in sorted order.

This script will work with any version of SharePoint 2010.


#Add-PSSnapin Microsoft.SharePoint.PowerShell 
#Set the $ErrorActionPreference to "STOP" so that the script stops if there are any errors 
$ErrorActionPreference = "STOP" 
#Declare the constants 
$webURL = "http://YourSiteHere" 
$listName = "newlistforcase" 
#Create a sorted list to hold the views 
$sortedList = new-object System.Collections.SortedList 
$web = Get-SPWeb $webURL 
$list = $web.Lists[$listName$views = $list.Views 
#Go through all the views and add them to the sorted list, so that will automatically get sorted 
foreach ($currentView in $list.Views) 
    $sortedList.Add($currentView.Title, $currentView.Title) 
#At this point we have all the views sorted in the $sortedList object. Let's delete each view and add it again. 
foreach ($currentKey in $sortedList.Keys) 
    $viewToDelete = $list.Views[$currentKey] 
    #Add the new view 
    Write-Host "Cloning the view " $viewToDelete.Title 
    $newVew = $viewToDelete.Clone($viewToDelete.Title, $viewToDelete.RowLimit, $viewToDelete.Paged, $viewToDelete.DefaultView) 
    #Delete the old view only if new view got created 
    Write-Host "Deleting the view " $viewToDelete.Title 
#Set #ErrorActionPreference back to "Continue" 
$ErrorActionPreference = "Continue"