Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

How to sort View List DropDown in SharePoint 2010

5.00/5 (1 vote)
10 Feb 2011CPOL3 min read 42.3K  
How to sort the listviews you have created in SharePoint.

Have you ever wondered where to find the setting to sort ListViews you create in SharePoint? That drop down beside the list name that shows all the views available for that list? Well, if you are looking for it, then you can search and search forever until the new version of SharePoint comes out, and still won't find it, because there is no such setting available and it will always default to the creation date. If you don’t know what I am talking about, look at the image below and I guess that would explain.

Image 1

Based on that image, I have created the following in chronological order, but I want to sort it out alphabetically which makes more sense to users. But how would I do that? First, the quick and dirty way.

All you have to do is change the creation date of the View in SQL. So get the list ID first by going to the list settings, and you will see it either in the address bar or on the Mobile section of the list properties. Take note of that.

Image 2

Now you have the list ID, all you have to do is to go to the database server for your SharePoint instance and go to the WSS_Content database and choose the AllWebParts table. You can select using this query:

SQL
SELECT *
FROM [WSS_Content].[dbo].[AllWebParts] 
WHERE tp_ListId = <span style="color: red;">'Your List ID'
AND tp_Deleted = 0
ORDER BY tp_CreationTime

Now this will show you all the views on that list. And you will notice it has the same order in that drop down. From here, you can edit the dates to your liking to show the Aaphabetical sorting that you need. This is a bit of a hack and dirty version of solving the problem, it may also not be supported by Microsoft. But if you want the proper solution, then move on.

Image 3

Now since you moved on, I guess you are interested in getting the right solution which is by using SharePoint PowerShell. So we will be doing the same thing above but in a proper manner as we won't go editing dates one by one but we will be cloning each View, sorting them, then deleting the old items and recreating according to the sort order. So in the database, it’s still sorted by Date Created, but since we are creating it in an alphabetical fashion, it will show as such. Now I guess you will be using this a lot so we have to make this like an executable file so you can reuse it on any site / team sites and lists that you may have. So lets start by creating out the PowerShell script, copy and paste the code below, and save it as Sort.ps1.

#Make the Script will Stop on errors
$ErrorActionPreference = "Stop"

#Get the variables needed by the following prompts
$TeamSitePrompt = Read-Host "What is the Team Site URL (i.e. http://test.com/teamsite)"
$ListNamePrompt = Read-Host "What is the List Name you want to sort" 

#Create a Sorted List to store the Views you want to Sort
$SortedList = New-Object System.Collections.SortedList

$Web = Get-SPWeb $TeamSitePrompt
$List = $Web.Lists[$ListNamePrompt]
$Views = $List.Views

#Loop through the Views on the List and Save then on the Sorted List 
#This will be automatically sorted as the Object is System.Collections.SortedList
Write-Host "Saving the List " $ListNamePrompt " to a Sorted Collection"
foreach ($CurrentViewItem in $List.Views)
{
 $SortedList.Add($CurrentViewItem.Title, $CurrentViewItem.Title)
}
Write-Host "---------------------------------------"

#Now the List is Sorted we Delete the View Item and Create it Again
#This makes it look like the Views are listed
#alphabetically but they are still listed by Creation Date
#Were just recreating them in alphabetical manner
foreach ($CurrentKeyItem in $SortedList.Keys)
{
 Write-Host "Processing Current View " $ViewToDelete.Title
 $ViewToDelete = $List.Views[$CurrentKeyItem]

 #Dulpicate the Current View
 Write-Host "Duplicating Please wait ..."
 $NewVew = $ViewToDelete.Clone($ViewToDelete.Title, 
               $ViewToDelete.RowLimit, $ViewToDelete.Paged, 
               $ViewToDelete.DefaultView)

 #Delete the Old View, the New View will have a New ID so we use ID to Delete
 Write-Host "Deleting the old View"
 $List.Views.Delete($ViewToDelete.ID)

 Write-Host "---------------------------------------"
 }

#Set $ErrorActionPreference back to "Continue"
$ErrorActionPreference = "Continue"
Write-Host "------- F I N I S H E D --------"

Now you have created the script; copy it over to you SharePoint server and run the PowerShell script by going to SharePoint 2010 Management Shell. Follow the prompts.

Image 4

Once that’s done, all your views are sorted; to double-check, run your SQL query above again and see what is recorded on the database.

Image 5

You can see that your views are sorted alphabetically:

Image 6

But wait, you might see the “Webpage cannot be found” page; don’t worry, your list was not corrupted, this happens when you have a lot of views configured and SharePoint is still processing this in the background; wait for it, or have a cup of coffee, and when you go back, hopefully it shows :)

Image 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)