This post is about how to integrate a Silverlight Webpart in SharePoint 2013 and use the Sharepoint Client Object Model, to show SharePoint Data.
The goal is to create a Silverlight Webpart that queries data to QuickLaunch Navigation and shows the result in a two levels TreeView Control.
I started creating a SharePoint 2013 Silverlight Webpart Project in Visual Studio 2012 (named SilverlightSample).
It automatically created a SilverlightWebpart (SilverlightSampleWebPart).
Then I starded with MainPage.xaml, where I inserted a TreeView Control, and created the templates to render data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| < UserControl xmlns:Client = "clr-namespace:Microsoft.SharePoint.Client;assembly=Microsoft.SharePoint.Client.Silverlight" x:Class = "SilverlightSampleWebPart.MainPage" mc:Ignorable = "d" d:DesignHeight = "200" d:DesignWidth = "120" > < UserControl.Resources > < sdk:HierarchicalDataTemplate x:Key = "ChildrenTemplate" ItemsSource = "{Binding Path=Children}" > < TextBlock Text = "{Binding Path=Title}" /> </ sdk:HierarchicalDataTemplate > </ UserControl.Resources > < Grid x:Name = "LayoutRoot" Background = "White" Height = "200" Width = "120" > < sdk:TreeView Grid.Row = "2" Name = "treeView" ItemTemplate = "{StaticResource ChildrenTemplate}" > </ sdk:TreeView > </ Grid > </ UserControl > |
Next I edited MainPage.xaml.cs file, to put the logic to get Data.
I created a class SiteNodes, where the first node is set to Web Title, and the Children nodes are the first level navigation nodes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| 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 SilverlightSampleWebPart { public partial class MainPage : UserControl { private ClientContext context; private NavigationNodeCollection nav; SiteNodes site; public class SiteNodes { public string Title { get; set; } public List< SiteChildren > Children { get; set; } } public class SiteChildren { public string Title { get; set; } } public MainPage() { InitializeComponent(); context = new ClientContext(ApplicationContext.Current.Url); context.Load(context.Web); nav = context.Web.Navigation.QuickLaunch; context.Load(nav); context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null); } private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args) { // This is not called on the UI thread. Dispatcher.BeginInvoke(BindData); } private void BindData() { List< SiteNodes > siteColl = new List< SiteNodes >(); site = new SiteNodes { Title = context.Web.Title }; site.Children = new List< SiteChildren >(); foreach (NavigationNode nn in nav) { BindChilds(nn); } siteColl.Add(site); treeView.ItemsSource = siteColl; } private void BindChilds(NavigationNode nn) { SiteChildren sn = new SiteChildren{ Title = nn.Title }; site.Children.Add(sn); } private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs< object > e) { } } } |
After that I deployed my solution and add my new SilverlightWebpart to my page:
No comments:
Post a Comment