Should I create my own object model to handle complex Sharepoint objects?

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  //Custom Object
                {
                    Name = folder.Name,
                    Children = (from SPFolder subFolder in folder.SubFolders //Further looping of sub folders                                       
                                select new Folder
                                {
                                     Name = subFolder.Name,
                                     Items = (from SPFile file in subFolder.Files
                                              select new Item
                                               {
                                               //Mapping code omitted
                                               }                                             ).ToList()
                                } 
                                        {)
                }

    return query.ToList();
+3
source share
5 answers

For your specific purpose, what is wrong (*) when you get SPList.RootFolder from the list and work from there, recursively moving subfolders and items in each folder?

(*) ...

+1
+5

, , SharePoint. , SharePoint OM . , , .

( ) , , SharePoint.

, . "" / , , .

( ) (, , BusinessGroups/Marketing , "" BusinessGroup.)

SharePoint , "" , . .

+1

What I did was check to see if the parent of the item was in the list that I was looking at and work from there. A class SPQuerycan also be a good way. Good luck

0
source

To add Lars (which is the correct answer) to the answer, if you feel the need to change the object model, try and solve your problem using the extension method. I did it with some success,

0
source

Source: https://habr.com/ru/post/1705953/


All Articles