I'm trying to do something pretty simple, but it seems like an almost impossible task with the SharePoint API.
My SharePoint data structure is similar:
-Folder
--- Subfolder
-------- Item A
-------- Item B
-------- Item C
-------- Item D
For some strange reason, you cannot access the folder and subfolders in the hierarchical manner you expect! When I iterate over a list, it returns all the elements, ignoring the hierarchical structure (i.e. returns everything in the list). Worse, you don't know if an item is a folder or not to manage the structure in the code.
Now I am writing custom objects to make the SharePoint object model more meaningful and group the data in the hierarchy that I expect. I plan to have my SharePoint items displayed as follows:
public class Folder
{
public Folder Parent {get; set;}
public Folder Root {get; set;}
public IList<Item> Items {get; set;}
}
Has anyone done something similar or how did you deal with this limitation in SharePoint?
Are there any lessons learned and all that needs to be considered if I start matching my custom object model?
EDIT:
My final solution was to loop through the folder too, starting with list.RootFolder.SubFolders.
var query = from SPList list in Utils.GetList(webRelativeUrl, listName)
from SPFolder folder in list.RootFolder.SubFolders
where folder.Name.ToLower() != "forms"
select new Folder
{
Name = folder.Name,
Children = (from SPFolder subFolder in folder.SubFolders
select new Folder
{
Name = subFolder.Name,
Items = (from SPFile file in subFolder.Files
select new Item
{
} ).ToList()
}
{)
}
return query.ToList();
Th3fix3r
source
share