Friday 19 July 2013

Sharepoint 2013 Create a Visual Web Part

n Visual Studio 2012, create a new SharePoint 2013 Empty Project:
app1
Choose your dev SharePoint Site and deploy as a farm solution:
Add a Visual Web Part item to your project:
app3
Edit your Visual Web Part, and insert a calendar and a label(choose from toolbox):
app4
Define the calendar’s selected date changed event, by double clicking any date in your calendar, and change the event code:
protectedvoid Calendar1_SelectionChanged(object sender, EventArgs e)
{
this.Label1.Text = Calendar1.SelectedDate.ToString();
}
Deploy your solution, and add the Visual Webpart to your Home page:
app5

Sharepoint 2013 Silverlight Webpart

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:
Silverlight