CAML Query for retrieveing ListItems in the hierarchal order
I was wondering if it possible to order listitems coming back from SharePoint (using Client Object API) so that they are ordered hierarchly, and not in the order they were created.
This is need to keep the folder structure intact. So for example if this was a structure:
folder001
-folder001_sub001
--item001
--item002
-folder002_sub002
--folder002_sub002_sub001
---item003
--item004
-item005
would be returned in the same order or at least this way (items first, then folders):
folder001
-item005
-folder001_sub001
--item001
--item002
-folder002_sub002
--item004
--folder002_sub002_sub001
---item003
Is this possible 开发者_StackOverflowwith CAML query? Default is to return things in order they were created (by index number inside the list)
Thank you in advance!
I would rather go with client object model instead of caml in this scenario. You could do something like this (untested!):
void RecursiveListFolder(Folder folder, int indent = 0)
{
clientContext.Load(folder.Folders);
clientContext.ExecuteQuery();
foreach (Folder folder in folder.Folders)
{
Console.WriteLine(new string(' ', indent) + folder.Name);
RecursiveListFolder(folder, indent + 1);
}
clientContext.Load(folder.Files);
clientContext.ExecuteQuery();
foreach (File file in folder.Files)
{
Console.WriteLine(new string(' ', indent) + file.Name);
}
}
This method would be used in this way:
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
clientContext.Load(web.Lists);
clientContext.ExecuteQuery();
List list = web.Lists["MyList"];
RecursiveListFolder(list.RootFolder);
精彩评论