Silverlight treeview control for SharePoint 2010

In this article we will be seeing how to use Silverlight tree view control in SharePoint 2010.

I have created a Silverlight tree view which is used to display all the lists available in the site as shown in the following.


"Sample" is the name of the site. When you expand the site you could see all the lists available for the list.


<sdk: TreeView/>

Steps Involved:

Creating a Silverlight Application:

  1. Open Visual Studio 2010.
  2. Go to File => New => Project.
  3. Select Silverlight from the Installed templates and choose the Silverlight Application template.
  4. Enter the Name and choose the location.
  5. Click OK.
  6. In the New Silverlight Application wizard uncheck the "Host the Silverlight Application in a new Web site".
  7. Click OK.

Creating the UI :

Open MainPage.xaml file and replace the code with the following one.

<UserControl x:Class="TreeviewControl.MainPage"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="">

    <Grid x:Name="LayoutRoot" Background="Gray">
        <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="0,47,0,0" Name="treeView" VerticalAlignment="Top" Width="400" >
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                        <TextBlock Text="{Binding Title}" Foreground="Green"   />

Open MainPage.xaml.cs file and replace the code with the following one.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
namespace TreeviewControl
    public partial class MainPage : UserControl
        private ClientContext context;
        private ListCollection listColl;
        public class Site
            public string Title { get; set; }
            public List<SiteLists> Children { get; set; }

        public class SiteLists
            public string Title { get; set; }

        public MainPage()
            context = new ClientContext(ApplicationContext.Current.Url);
            listColl = context.Web.Lists;
            context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

        private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
            // This is not called on the UI thread.

        private void BindData()
            List<Site> siteColl = new List<Site>();
            Site  site= new Site { Title = context.Web.Title };
            site.Children = new List<SiteLists>();
            foreach (List list in listColl)
                SiteLists siteLists = new SiteLists { Title = list.Title };
            this.treeView.ItemsSource = siteColl; 

Build the solution.

Deploying in SharePoint site:

  • Upload the xap file in the SharePoint Site => Shared Documents.

  • Go to Site Actions => Edit Page => Editing Tools => Insert => Web Part.


  • Go to Categories => Media and Content => Silverlight Web Part => Click on Add.


  • In the URL give the path of the xap file ~site/Sample/Shared%20Documents/TreeviewControl.xap


  • Click on Ok.

Similar Articles