Monday, January 5, 2009

How Can I Show All Items with/without Folders in SharePoint?

To implement this we have 3 options

Option 1
Using SPQuery object and setting ViewAttributes property in it; You can retrieve only the folders from the SharePoint List, with a single condition.

SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Shared Documents"];
SPQuery query = new SPQuery();
//Condition to check the item type is folder or not
query.Query = "<Where><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></Where>";

//Get all the items including subfolders from the list
query.ViewAttributes = “Scope=’RecursiveAll’";
//Retrieve the items based on Query SPListItemCollection items = list.GetItems(query);

string folderDetails="";
//Get the name and Url for the folder
foreach (SPListItem item in items)
folderDetails += "Folder Name:" + item.Name + "<br/>Folder URL:" + web.Url + "/" + item.Url + "<br/>";

In Query property of SPQuery object, You can set 0 for folder (if item type  = folder) or 1 for List Item/Documents(if ItemType=List Item).

We have set Scope=’RecursiveAll’, which allows to retrieve all the items and folders from the list or library.

Option 2
1. Open Data Source Details pane in Menu > Task Panes > Data Source Details.
2. Click the data source listed in Current Data Source.
3. In the opening Data Source Properties pane, change the item and folder scope from Files Only to Recursive
4. Save and exit.

Option 3
Other than dataview webpart you can also set options directly in views also. To implement this we can take example of "All Documents", follow the steps1. Select "All Documents" view
2. Select "Modify This View"
3. Scroll down whole page
4. Expand Folders
5. Then select "Show all items without folders"
6. Click OK button