Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

View SharePoint List Items in a TreeView

4.00/5 (1 vote)
9 Aug 2013CPOL3 min read 25.5K   370  
This article shows how to iterate through a SharePoint list's hierarchy and display the items in an ASP.NET webform's TreeView control.

Introduction

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.

Background

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>

C#
const string serverURL = "http://[YOUR SHAREPOINTSITE]/";
public string ServerURL
{
    get
    {
        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.

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

    //Load Libraries from SharePoint
    clientcontext.Load(clientcontext.Web.Lists);
    clientcontext.ExecuteQuery();
    foreach (List list in clientcontext.Web.Lists)
    {
      ddlLists.Items.Add(list.Title);
    }
  }
}

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.

C#
protected void btnLoad_Click(object sender, EventArgs e)
{
  using (ClientContext clientcontext = new ClientContext(ServerURL))
  {
    List list = clientcontext.Web.Lists.GetByTitle(ddlLists.SelectedItem.Text);
    clientcontext.Load(list);
    clientcontext.ExecuteQuery();
    clientcontext.Load(list.RootFolder);
    clientcontext.Load(list.RootFolder.Folders);
    clientcontext.ExecuteQuery();
    TreeViewLibraries.ShowLines = true;
    TreeNode RootNode = new TreeNode(list.Title);
    TreeViewLibraries.Nodes.Add(RootNode);

    IterateListItems(RootNode, list.RootFolder, clientcontext);

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

  }
  TreeViewLibraries.ExpandAll();
}

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

C#
public void IterateListItems(TreeNode MainNode, Folder folder, ClientContext clientcontext)
{
  clientcontext.Load(folder.Files);
  clientcontext.ExecuteQuery();
  foreach (File file in folder.Files)
  {
    TreeNode SubNode = new TreeNode(file.Name);
    MainNode.ChildNodes.Add(SubNode);
  }
}

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.

C#
public void IterateFolders(TreeNode MainNode, Folder subFolder, ClientContext clientcontext)
{
  clientcontext.Load(subFolder.Folders);
  clientcontext.ExecuteQuery();
  foreach (Folder folder in subFolder.Folders)
  {
    TreeNode SubNode = new TreeNode(folder.Name);
    MainNode.ChildNodes.Add(SubNode);
    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.

License

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