top of page

Silverlight tree view 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.

Xaml:

<sdk: TreeView/>

Steps Involved:

Creating a Silverlight Application:

  • Open Visual Studio 2010.

  • Go to File => New => Project.

  • Select Silverlight from the Installed templates and choose the Silverlight Application template.

  • Enter the Name and choose the location.

  • Click OK.

  • In the New Silverlight Application wizard uncheck the "Host the Silverlight Application in a new Web site".

  • Click OK.


Creating the UI :

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

  1. <UserControl x:Class="TreeviewControl.MainPage"

  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300"

  6. d:DesignWidth="400"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">


  1. <Grid x:Name="LayoutRoot" Background="Gray">

  2. <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="0,47,0,0"Name="treeView" VerticalAlignment="Top" Width="400" >

  3. <sdk:TreeView.ItemTemplate>

  4. <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}" >

  5. <StackPanel>

  6. <TextBlock Text="{Binding Title}" Foreground="Green"  />

  7. </StackPanel>

  8. </sdk:HierarchicalDataTemplate>

  9. </sdk:TreeView.ItemTemplate>

  10. </sdk:TreeView>

  11. </Grid>

  12. </UserControl>

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

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Net;

  5. using System.Windows;

  6. using System.Windows.Controls;

  7. using System.Windows.Documents;

  8. using System.Windows.Input;

  9. using System.Windows.Media;

  10. using System.Windows.Media.Animation;

  11. using System.Windows.Shapes;

  12. using Microsoft.SharePoint.Client;

  13. namespace TreeviewControl

  14. {

  15. public partial class MainPage : UserControl   

  16. {

  17. private ClientContext context;

  18. private ListCollection listColl;

  19. public class Site       

  20. {

  21. public string Title { get; set; }

  22. public List<SiteLists> Children { get; set; }       

  23. }

  24. public class SiteLists       

  25. {

  26. public string Title { get; set; }       

  27. }

  28. public MainPage()       

  29. {           

  30. InitializeComponent();           

  31. context = new ClientContext(ApplicationContext.Current.Url);           

  32. context.Load(context.Web);           

  33. listColl = context.Web.Lists;           

  34. context.Load(listColl);           

  35. context.ExecuteQueryAsync(newClientRequestSucceededEventHandler(OnRequestSucceeded), null);       

  36. }

  37. private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)       

  38. {

  39. // This is not called on the UI thread.           

  40. Dispatcher.BeginInvoke(BindData);       

  41. }

  42. private void BindData()       

  43. {

  44. List<Site> siteColl = new List<Site>();

  45. Site  site= new Site { Title = context.Web.Title };           

  46. siteColl.Add(site);           

  47. site.Children = new List<SiteLists>();

  48. foreach (List list in listColl)           

  49. {

  50. SiteLists siteLists = new SiteLists { Title = list.Title };               

  51. site.Children.Add(siteLists);           

  52. }

  53. this.treeView.ItemsSource = siteColl;         

  54. }   

  55. }

  56. }

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.

0 comments
bottom of page