1

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

...
spClientContext.Load(spFOlder.Folders) 

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
Norman

2 Answers 2

1

I know the OP referred to VB in the question but I had the same issue in C# and based on @Damjan's answer this is what worked for me:

var sorted = folder.Folders.OrderBy(t => t.Name);

Also had to add

Using System.Linq;
1

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

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

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

Add this in the top section in the code

Imports System.Linq

Then the code below:

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

Dim sorted = folders.OrderBy(Function(t) t.Name)
For Each f In sorted
    Console.WriteLine(f.Name)
Next
4
  • 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, 2020 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, 2020 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, 2020 at 16:14
  • Cool, now this works, too: spClientContext.Load(spFOlder.Folders, Function(folders) folders.OrderBy(Function(folder) folder.Name))
    – Norman
    Feb 24, 2020 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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