Click here to Skip to main content
13,044,543 members (79,198 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 9 Aug 2013

View SharePoint List Items in a TreeView

, 9 Aug 2013
Rate this:
Please Sign up or sign in to vote.
This article shows how to iterate through a SharePoint list's hierarchy and display the items in an ASP.NET webform's TreeView control.


SharePoint is expanding its horizons these days and there is no chance for a web developer to ignore any possibilities to integrate with SharePoint. Due to its wide functionality, sometimes even common ASP.NET applications too are required to integrate with SharePoint sites in organisations. Sometimes information is needed to be fetched from a SharePoint site. In this article, I will discuss one such scenario where we bring the structure of a SharePoint List/Library to be rendered in a TreeView control of an ASP.NET webpage.


For those who haven't explored SharePoint yet, SharePoint is a web application platform that comprises a multipurpose set of Web technologies backed by a common technical infrastructure. SharePoint can be used to provide intranet portals, document & file management, collaboration, social networks, extranets, websites, enterprise search, and business intelligence. It also has system integration, process integration, and workflow automation capabilities. A SharePoint application can consist of a collection of Sites. The basic structure to store information is in Lists and Libraries. A List can be thought of as a collection of pieces of information — all of which (typically) have the same properties. This could be considered similar to a database table. For instance, you can have a list of links called "my links", where each item has a URL, a name, and a description.<o:p />

The .NET Managed client object model (OM) is one of the Microsoft SharePoint 2010 Software Development Kit (SDK)’s three new client APIs that allow you to interact with SharePoint sites. It provides a subset of the server object model that is defined in Microsoft.SharePoint.dll. The object models provide a consistent and easy-to-use, object-oriented system for interoperating with SharePoint data from a remote client or server.<o:p />

Two assembly references are required to be added to a Visual Studio project for using .NET Managed Client OM - Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.<o:p />

Using the code

This code uses an ASP.NET webform where we have a TreeView control and a DropDownList added. The dropdownlist is used to populate the names of all lists in the SharePoint site mentioned (site URL to be provided in the constant string).<o:p />

const string serverURL = "http://[YOUR SHAREPOINTSITE]/";
public string ServerURL
        return serverURL;

Now on page load, we populate the DropDownList with the title names of the Lists in the Sharepoint site. Note that we use the client object model here as ClientContext. We need to provide the URL of the SharePoint site to the ClientContext object.

protected void Page_Load(object sender, EventArgs e)
  using (ClientContext clientcontext = new ClientContext(ServerURL))

    //Load Libraries from SharePoint
    foreach (List list in clientcontext.Web.Lists)

After selecting the required SharePoint List name in the dropdownlist, the user has to click on the Load button in the form. This event is used to populate the TreeView control.

protected void btnLoad_Click(object sender, EventArgs e)
  using (ClientContext clientcontext = new ClientContext(ServerURL))
    List list = clientcontext.Web.Lists.GetByTitle(ddlLists.SelectedItem.Text);
    TreeViewLibraries.ShowLines = true;
    TreeNode RootNode = new TreeNode(list.Title);

    IterateListItems(RootNode, list.RootFolder, clientcontext);

    foreach (Folder SubFolder in list.RootFolder.Folders)
      if (SubFolder.Name != "Forms")
        TreeNode TopNode = new TreeNode(SubFolder.Name);
        IterateListItems(TopNode, SubFolder, clientcontext);
        IterateFolders(TopNode, SubFolder, clientcontext);


Here is the code that lists out the list item names to the tree nodes:

public void IterateListItems(TreeNode MainNode, Folder folder, ClientContext clientcontext)
  foreach (File file in folder.Files)
    TreeNode SubNode = new TreeNode(file.Name);

This is the recursive method for iterating through the folders in the list. It visits each folder, updates the folder name to the treeview, iterates through the list items, and then iterates to its subfolders digging down.

public void IterateFolders(TreeNode MainNode, Folder subFolder, ClientContext clientcontext)
  foreach (Folder folder in subFolder.Folders)
    TreeNode SubNode = new TreeNode(folder.Name);
    IterateListItems(SubNode, subFolder, clientcontext);
    IterateFolders(SubNode, folder, clientcontext);

Points of Interest

Some times you may get some authentication failures while executing this code. Try to run Visual Studio as Administrator and also check whether you have proper rights in the SharePoint Site.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jobin Daniel
Technical Lead
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionproblem with your code on application page Pin
Member 405964823-May-17 21:58
memberMember 405964823-May-17 21:58 
QuestionNice Pin
DanielBrownAU20-Jul-16 17:23
professionalDanielBrownAU20-Jul-16 17:23 
QuestionNice Code Pin
Member 375157320-Jul-16 15:42
memberMember 375157320-Jul-16 15:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 9 Aug 2013
Article Copyright 2013 by Jobin Daniel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid