Sorting SharePoint 2010 SPFolders and SPFiles

Sort SP Folders

// folder is some SPFolder, e.g. SPList.RootFolder
List spFolders = folder.SubFolders.Cast().ToList();
spFolders.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));

foreach (SPFolder subFolder in spFolders)
{
	// Your code here
}

Sort SP Files

// folder is some SPFolder, e.g. SPList.RootFolder
List spFiles = folder.Files.Cast().ToList();
spFiles.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));

foreach (SPFile file in spFiles)
{
	// Your code here
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami