1

I have a Sandbox webpart which retrieves a collection of Folders in a Library.

The library contains the following sample data:

|   Name        |   Created   |
-------------------------------
|    Folder 1   |  05/06/2013 |
|    Folder 2   |  07/08/2014 |
|    Folder 3   |  05/06/2015 |
|    Folder 4   |  04/03/2016 |
|    Folder 5   |  09/09/2016 |
|    ...        |    ...      |

I need retrieve these results ordered by Created field.


I have the following code for retrieve the collection of folders and sort its results:

string ImageLibrary = "GalleryLibrary";

// Get folders in the image library.
SPFolderCollection coleccionAlbums = (SPFolderCollection)_web.Folders[ImageLibrary].SubFolders;
List<SPFolder> spFolders = new List<SPFolder>();

// Create a new List<SPFolder> for store folders in DESCENDING order.
foreach (SPFolder fl in colAlbums) { spFolders.Add(fl); }

And I use any of the following code for order the elements in the list:

spFolders.Reverse();
// or
spFolders.Reverse(0, spFolders.Count);

However, the previous methods order the items by name, not by Date.

I make this change, but the Created property is not found:

spFolders.Sort((f1, f2) => f1.Item["Created"].ToString().CompareTo(f2.Item["Created"].ToString()));

How order a List of SPFolder by Created field?

  • SPFolderCollection coleccionAlbums = (SPFolderCollection)web.Folders[ImageLibrary].SubFolders; List<SPFolder> spFolders = new List<SPFolder>(); foreach (SPFolder fl in coleccionAlbums) { if (fl.Item != null) { spFolders.Add(fl); } } spFolders.Sort((f1, f2) => f1.Item.Folder.Item["Created"].ToString().CompareTo(f2.Item.Folder.Item["Created"].ToString())); – Prasanna Nov 25 '16 at 0:32
  • @Prasanna thank you for your comment, but you should post it as an answer :) – Mauricio Arias Olave Nov 25 '16 at 13:12
1

Using:

 using System.Linq;

Try this:

spFolders.OrderByDescending(f => f.Item[SPBuiltInFieldId.Created]);

or this (faster):

spFolders.OrderByDescending(f => f.Item.ID);

or this:

SPQuery query = new SPQuery();
query.Query = @"<Where>
                    <Eq>
                        <FieldRef Name='FSObjType' />
                        <Value Type='Integer'>1</Value>
                    </Eq>
                </Where>
                <OrderBy>
                    <FieldRef Name='Created' Ascending='False'/>
                </OrderBy>";

string ImageLibrary = "GalleryLibrary";
SPList list = _web.Lists.TryGetList(ImageLibrary);
if (list == null) return;

List<SPFolder> spFolders = new List<SPFolder>();
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
    spFolders.Add(item.Folder);
}
  • Thank you for your help. List<SPFolder> doesn't have OrderBy method, but I'm testing a variations of your code and see it if works. I also check this answer. – Mauricio Arias Olave Nov 24 '16 at 22:55
  • I changed the answer to make it Descending.add using System.Linq; – Cecilia Nov 24 '16 at 22:59
  • I changed the answer to make it Descending. Add using System.Linq; for the OrderBy method – Cecilia Nov 24 '16 at 23:00
  • Cecilia, I used the CAML Query approach you posted and it worked wonders. Thank you and Welcome to SharePoint Stack Exchange. – Mauricio Arias Olave Nov 25 '16 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.