How to sort View List DropDown in Sharepoint 2010

By | February 7, 2011

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 🙂


Recommended

16 thoughts on “How to sort View List DropDown in Sharepoint 2010

  1. Pingback: Tweets that mention How to sort View List DropDown in Sharepoint 2010 « Raymund Macaalay's Dev Blog -- Topsy.com

  2. Loni

    Is there modification that I need to do to make this work for a Library? I’m getting an error:

    PS P:> C:Users####DesktopSort.ps1
    What is the Team Site URL (i.e. http://test.com/teamsite): http://####
    What is the List Name you want to sort: Load Documents
    Saving the List Load Documents to a Sorted Collection
    Exception calling “Add” with “2” argument(s): “Key cannot be null.
    Parameter name: key”
    At C:Users####DesktopSort.ps1:20 char:17
    + $SortedList.Add <<<

    Reply
  3. rsmacaalay

    For this line
    foreach ($CurrentViewItem in $List.Views)
    {
    $SortedList.Add($CurrentViewItem.Title, $CurrentViewItem.Title)
    }

    can you do this instead?

    foreach ($CurrentViewItem in $List.Views)
    {
    Write-Host $CurrentViewItem.Title
    $SortedList.Add($CurrentViewItem.Title, $CurrentViewItem.Title)
    }

    Looks like it does not have a value for $CurrentViewItem.Title

    Reply
  4. Dilip Sant

    Hi, I am using this script and recieve the below error. We do have lot of Private Views

    Exception calling “Add” with “2” argument(s): “Item has already been added. Key in dictionary: ” Key being added: ””
    At C:listviews.ps1:21 char:17
    + $SortedList.Add <<<< ($CurrentViewItem.Title, $CurrentViewItem.Title)
    + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordE
    xception
    + FullyQualifiedErrorId : DotNetMethodException

    Reply
    1. Loni

      I had a similar problem. What I had to do was specify

      if($CurrentViewItem.Title != “”)
      {
      add script stuff here
      }
      I’m trying to recall off the top of my head what the proper syntax is, but it’s difficult since I don’t have the script in front of me right now.

      Reply
      1. Shirley Griffin

        Where did you add this line? I’m getting the same error message as mentioned here.

        Reply
      2. Loni

        $a = “”
        foreach ($CurrentViewItem in $List.Views)
        {
        if ($CurrentViewItem.Title -ne $a)
        {
        $SortedList.Add($CurrentViewItem.Title, $CurrentViewItem.Title)
        }
        }

        Reply
      3. Loni

        I suppose I should have specified that it was just after the declaration of $Views = $List.Views that I put the change since that’s actually the question that you asked.

        Reply
  5. Robin

    I am having the same problem:
    Exception calling “Add” with “2″ argument(s): “Item has already been added. Key cannot be null.
    Parameter name: key”
    At c:spdocssort-list.ps1:21 char:17
    + $SortedList.Add <<<< ($CurrentViewItem.Title, $CurrentViewItem.Title)
    + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordE
    xception
    + FullyQualifiedErrorId : DotNetMethodException

    I added the line above to write host currentviewitem but nothing shows. Why does it think the arguments are null?

    Reply
  6. Pingback: Todd Klindt's SharePoint Admin Blog

  7. Pingback: SharePoint MVP Blogs

  8. Pingback: 3 Ways To Reorder Views in SharePoint 2010 | Code-Journey.com

  9. Pingback: SharePoint for End Users & List View Reordering | SharePoint Blog ~ S. J. Huibregtse

  10. Pingback: Sharepoint 2010 – Views in alphabetischer Reihenfolge | derheld.de

  11. Mathew Newton

    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

    Reply

Leave a Reply to Shirley GriffinCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.