I do have the folders and they are loaded correctly but they are not sorted.

dim spClientContext As Microsoft.SharePoint.Client.ClientContext
dim spFOlder As Microsoft.SharePoint.Client.Folder


Sorry for my question, I am a newbie: How can I sort spFolder.Folders by name?

I need it in VB.NET.

Thanks in advance


You should be able to sort it as any other collection of objects. This piece of code should do the work:

Dim sorted = spFOlder.Folders.OrderBy(Function(t) t.Name)

UPDATE (code that worked in VB.NET Console Application): ... login code ommited

Dim web = Context.Web
Dim list = web.Lists.GetByTitle("LIST TITLE")
Dim folders = list.RootFolder.Folders

Dim sorted = folders.OrderBy(Function(t) t.Name)
For Each f In sorted
  • Thank you very much, I tried that but OrderBy is not defined for folders. I saw that and OrderByDescending on the net but my Microsoft.SharePoint.Client.Folder is missing that. Do I habe to import something that I dont have yet? I looked in the docs, too, and Order by doesn't seem to be available for Microsoft.SharePoint.Client.Folder. Guess I am missing something...
    – Norman
    Feb 24 '20 at 14:24
  • OrderBy (and OrderByDescending) are extension methods from System.Linq.Queryable class and should be automatically available on FolderCollection. I've tested the code in VB.NET Console Application and it worked fine. I've updated my answer with my code so you can check if you can reuse any part of it. Feb 24 '20 at 16:06
  • 1
    Oh you are brilliant, thank you very much :) I added this: Imports System.Linq.Queryable, that did the trick. should have imported the clients stuff, probably would have worked from start then. Thanks Damjan!
    – Norman
    Feb 24 '20 at 16:14
  • Cool, now this works, too: spClientContext.Load(spFOlder.Folders, Function(folders) folders.OrderBy(Function(folder) folder.Name))
    – Norman
    Feb 24 '20 at 16:20

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.