Click here to Skip to main content
11,482,039 members (67,181 online)
Click here to Skip to main content

How to sort View List DropDown in SharePoint 2010

, 10 Feb 2011 CPOL 17.5K 2
Rate this:
Please Sign up or sign in to vote.
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.

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.

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:

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.

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.

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.

You can see that your views are sorted alphabetically:

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 Smile | :)

License

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

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionView list sorted with creating some other issues Pin
sushil_online22-May-12 1:09
membersushil_online22-May-12 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 10 Feb 2011
Article Copyright 2011 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid