About these ads

How to sort View List DropDown in Sharepoint 2010

Have you ever wondered where is the setting to sort the List Views you created 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 wont find it, because there is no such setting available and it will always default to 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 had created the following in Chronological Order, but I want to sort it out alphabetically which makes more sense to the users.  But how would I do that? First the quick and dirty way.

All you have to do is change the creation date of that 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 WSS_Content database and choose AllWebParts table.  You can select using this query

SELECT *
FROM [WSS_Content].[dbo].[AllWebParts] 
WHERE tp_ListId = '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 Alphabetical sorting that you need.  This is a bit of hack and a 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 wont go editing dates one by one but we will be cloning each View, sort them, then delete the old items and recreate according to sort order.  So in the database it’s still sorted by Date Created but since we are creating it in an alphabetical fashion then 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 on any site / team sites and lists that you may have.  So lets start by creating out powershell script, copy and paste that 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 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 then all your views are sorted, to double-check run again your SQL Query above and see whats recorded on the database.

You can also see you 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 at the background, wait for it or have a cup of coffee and when you  go back hopefully it shows :)

About these ads

16 Responses to How to sort View List DropDown in Sharepoint 2010

  1. Mathew Newton says:

    Hi,
    If you don’t have too many views you can use SharePoint Designer to copy each view aspx page and rename them back to the original name to reset the created date. If you do this process in a specific order you can group the views in any order you wish

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 197 other followers

%d bloggers like this: