SharePoint Config

Ari Bakker's thoughts on customising and configuring SharePoint

Displaying files from a specific folder using SPDataSource

without comments

This is a short post to show how you can use the SPDataSource to display items from a specific folder in a SharePoint document library. While the other parameters are fairly well documented on MSDN this isn’t very clear. Generally I don’t recommend the use of folders to separate data, I prefer a metadata based approach. In some cases, however, it makes sense such as when you need to manage permissions or approval for a group of documents (especially in SharePoint 2007 where you don’t have document sets). Looking at the parameters we can use (as shown below) to identify and locate the data it would appear that passing the name of the folder to the RootFolder property would achieve this.

Name Description
WebURL An empty string for the root Web site. Otherwise, a string containing a server-relative URL.
ListName The value of the SPList.Title property. You can also specify the list by setting the List property
ListItemID A string representation of an integer such as the value of the SPListItem.ID property.
RootFolder The value of the SPFolder.Name property.

Unfortunately setting RootFolder to the name of a specific folder such as “Folder1” doesn’t have any effect. The trick is to also specify the name of the list and the folder such as “Project Documents/Folder1” as shown below:

<SharePoint:SPDataSource ID="dsFolder"
    runat="server"
    IncludeHidden="true"
    DataSourceMode="List"
    UseInternalName="true"
    SelectCommand="&lt;View&gt;&lt;/View&gt;">
    <SelectParameters>
        <asp:Parameter Name="ListName" DefaultValue="Project Documents" />
        <asp:Parameter Name="RootFolder" DefaultValue="ProjectDocuments/Folder1" />
    </SelectParameters>
</SharePoint:spdatasource>

Also note that you can change the default behaviour to show both files and folders by specifying a value for the Scope property of the SPDataSource to one of the following SPViewScope values:

Member name Description
Default Show only the files and subfolders of a specific folder.
Recursive Show all files of all folders.
RecursiveAll Show all files and all subfolders of all folders.
FilesOnly Show only the files of a specific folder.

If you need the RootFolder parameter to be dynamic (e.g. based on metadata from the current page) you can use the technique described in my previous post on creating custom parameters for a SPDataSource. If you haven’t used the SPDataSource control before check out the SPDataSource documentation on MSDN. Chris O’Brian also has a good series titled SPDataSource every developers friend. It is a great way of quickly retrieving SharePoint data without writing C# code that can then be bound to ASP.NET controls such as the Repeater, ListView, GridView or DropDownList as well as the DataFormWebPart if you want to control the output using XSLT.

Post to Twitter Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Written by Ari Bakker

November 21st, 2010 at 4:17 pm

Leave a Reply

*