Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET
Tip/Trick

View SharePoint List Items in a TreeView

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
9 Aug 2013CPOL3 min read 25.1K   369   4   3
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)


Written By
Technical Lead
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionproblem with your code on application page Pin
Member 405964823-May-17 21:58
Member 405964823-May-17 21:58 
QuestionNice Pin
DanielBrownAU20-Jul-16 17:23
professionalDanielBrownAU20-Jul-16 17:23 
Nice and Simple.

Have you seen Offie PnP Core? GitHub - OfficeDev/PnP-Sites-Core: Office 365 Dev PnP Core component (.NET) targeted for increasing developer productivity with CSOM based solutions.[^]

Worth having a look at it imo
QuestionNice Code Pin
Member 375157320-Jul-16 15:42
Member 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.