Sunday, October 5, 2008

Retrieve SharePoint List With Folders and Files ( Tree Structure)

To retrieve SPList structure wise use SPQuery to retrieve childitems.

There is property called FileSystemObjectType of SPListItem that identifies the current item (Either Folder or File).

SPSite site = new SPSite("http://localhost:43588");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["WebLink List"];
Response.Write("<span style='padding-left:0px'>" + list.RootFolder.Name + "</span>" + "</br>");
GetChildItems(list, list.RootFolder,10);

GetChildItems is a recrusive function.

GetChildItems Function

private void GetChildItems(SPList lst , SPFolder folder,int padding)
   {
       SPQuery query = new SPQuery();
       query.Folder = folder;
       SPListItemCollection col =  lst.GetItems(query);
       foreach (SPListItem item in col)
       {
           if (item.FileSystemObjectType == SPFileSystemObjectType.File)
           {
               Response.Write("<span style='padding-left:"+ padding +"px'>"+item["URL"]+"</span>"+ "</br>");
           }
           else if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
           {               
               Response.Write("<span style='padding-left:" + padding + "px'>" + item.Title + "</span>" + "</br>");
               GetChildItems(lst, item.Folder,padding+30);
           }
       }
   }

2 comments:

Scott Way said...

there is no item.folder property in the sharepoint 2010 object model

dotnetstep said...

Please look at following link.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem_properties(v=office.14).aspx