ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Sunday, February 26, 2012

AddNew, Edit, Delete SharePoint List Item use Silverlight


A.      Thiết kế giao diện cho Silverlight
ü  Trong grid control thêm 5 dòng và 3 cột
<Grid.RowDefinitions>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="50"></RowDefinition>

           <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="200"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="100"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="100"></ColumnDefinition>

        </Grid.ColumnDefinitions>

ü  Dòng thứ nhất kéo TextBlock vào cột thứ nhất tên là textBlockTitle, TextBox vào cột thứ hai tên là txtTitle, Button vào côt thứ ba tên là  btnInsert
<!--row1-->

<TextBlock Name="textBlockTitle" Text="Title:" Grid.Row="0" Grid.Column="0" Height="30"></TextBlock>

<TextBox Name="txtTitle" Grid.Row="0" Grid.Column="1" Height="30"></TextBox>

<Button Name="btnInsert" Content="Insert" Grid.Row="0" Grid.Column="2" Click="btnInsert_Click" Height="30"></Button>

ü  Dòng thứ hai kéo TextBlock vào cột thứ nhất tên là textBlockIdUpdate, TextBox vào cột thứ hai tên là txtIdUpdate, Button vào côt thứ ba tên là  btnUpdate
<!--row2-->

<TextBlock Name="textBlockIdUpdate" Text="ID Update:" Grid.Row="1" Grid.Column="0" Height="30"></TextBlock>

<TextBox Name="txtIdUpdate" Grid.Row="1" Grid.Column="1" Height="30"></TextBox>

<Button Name="btnUpdate" Content="Update" Grid.Row="1" Grid.Column="2" Click="btnUpdate_Click" Height="30"></Button>

ü  Dòng thứ ba kéo TextBlock vào cột thứ nhất tên là textBlockIdDelete, TextBox vào cột thứ hai tên là txtIdDelete, Button vào côt thứ ba tên là  btnDelete
<!--row3-->

<TextBlock Name="textBlockIdDelete" Text="ID Delete:" Grid.Row="2" Grid.Column="0" Height="30"></TextBlock>

<TextBox Name="txtIdDelete" Grid.Row="2" Grid.Column="1" Height="30"></TextBox>

<Button Name="btnDelete" Content="Delete" Grid.Row="2" Grid.Column="2" Click="btnDelete_Click" Height="30"></Button>

ü  Dòng thứ tư kéo  Button vào côt thứ ba tên là  btnDisplay
<!--row4-->

<Button Name="btnDisplay" Content="Display" Grid.Row="3" Grid.Column="1" Height="30" Click="btnDisplay_Click"></Button>



ü  Dòng thứ năm kéo Gridview  vào côt thứ ba tên là  gridView

<!--row5-->       

<data:DataGrid Name="gridView" Grid.Row="4" Grid.ColumnSpan="3" Width="Auto" Height="200" Margin="8,0,0,0"/>

Source code giao diện

<UserControl x:Class="KnowledgeDatabase1.MainPage"

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

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

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

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="400" d:DesignWidth="400" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">







    <Grid x:Name="LayoutRoot" Background="White">

       

        <Grid.RowDefinitions>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="50"></RowDefinition>

            <RowDefinition Height="200"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="100"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="100"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <!--row1-->

        <!--row1-->

        <TextBlock Name="textBlockTitle" Text="Title:" Grid.Row="0" Grid.Column="0" Height="30"></TextBlock>

        <TextBox Name="txtTitle" Grid.Row="0" Grid.Column="1" Height="30"></TextBox>

        <Button Name="btnInsert" Content="Insert" Grid.Row="0" Grid.Column="2" Click="btnInsert_Click" Height="30"></Button>

        <!--row2-->

        <TextBlock Name="textBlockIdUpdate" Text="ID Update:" Grid.Row="1" Grid.Column="0" Height="30"></TextBlock>

        <TextBox Name="txtIdUpdate" Grid.Row="1" Grid.Column="1" Height="30"></TextBox>

        <Button Name="btnUpdate" Content="Update" Grid.Row="1" Grid.Column="2" Click="btnUpdate_Click" Height="30"></Button>

        <!--row3-->

        <TextBlock Name="textBlockIdDelete" Text="ID Delete:" Grid.Row="2" Grid.Column="0" Height="30"></TextBlock>

        <TextBox Name="txtIdDelete" Grid.Row="2" Grid.Column="1" Height="30"></TextBox>

        <Button Name="btnDelete" Content="Delete" Grid.Row="2" Grid.Column="2" Click="btnDelete_Click" Height="30"></Button>

        <!--row4-->

        <Button Name="btnDisplay" Content="Display" Grid.Row="3" Grid.Column="1" Height="30" Click="btnDisplay_Click"></Button>

        <!--row5-->       

        <data:DataGrid Name="gridView" Grid.Row="4" Grid.ColumnSpan="3" Width="Auto" Height="200" Margin="8,0,0,0"/>

    </Grid>

</UserControl>







Giao diện



B.      Code cho các sự kiện của button
ü  Hiển thị Sharepoint ListItem lên gridview trong Silverlight
ü  Thêm Sharepoint ListItem vào trong Sharepoint List
ü  Sửa Sharepoint ListItem trong Sharepoint List
ü  Xoá Sharepoint ListItem trong Sharepoint List
Các bạn double click vào từng button và viết code như sau:
1.       Add references đến 2 dll sau:

2.       Source code
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;

using SP = Microsoft.SharePoint.Client;
namespace KnowledgeDatabase1
{

    public partial class MainPage : UserControl

    {

        private List oList;

        private string siteUrl = "http://acer";

        int execute = 0;

        public MainPage()
        {

            InitializeComponent();

        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)

        {

            ClientContext clientContext = new ClientContext(siteUrl);

            Web oWebsite = clientContext.Web;

            ListCollection collList = oWebsite.Lists;
            oList = clientContext.Web.Lists.GetByTitle("DonXinNghiPhep");//Tên list

            ListItem oListItem = oList.AddItem(new ListItemCreationInformation());

            oListItem["Title"] = txtTitle.Text;//tên Field

            oListItem["TuNgay"] = DateTime.Now.ToShortDateString();//tên Field

            oListItem["DenNgay"] = DateTime.Now.ToShortDateString();//tên Field

            oListItem.Update();

            //Cho phep MessageBox.Show

            clientContext.Load(oList, list => list.Title);
            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            execute = 1;
        }

        private void onQuerySucceeded(object sender,ClientRequestSucceededEventArgs args)

       {
           UpdateUIMethod updateUI = DisplayInfo;
           this.Dispatcher.BeginInvoke(updateUI);
       }

        private void DisplayInfo()

        {

            if (execute == 1)

                MessageBox.Show("New item created in list: " + oList.Title);

            else
            {
                if (execute == 2)
                    MessageBox.Show("item updated in list: " + oList.Title);
                else
                    if (execute == 3)
                        MessageBox.Show("item deleted in list: " + oList.Title);
            }
        }

        private delegate void UpdateUIMethod();
        private void onQueryFailed(object sender, ClientRequestFailedEventArgsargs)
            MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);

        }
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
            oList = clientContext.Web.Lists.GetByTitle("DonXinNghiPhep");
            ListItem oListItem = oList.GetItemById(Convert.ToString(txtIdUpdate.Text));
            oListItem["Title"] = txtTitle.Text;
            oListItem.Update();
            //Cho phep MessageBox.Show
            clientContext.Load(oList, list => list.Title);
            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            execute = 2;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)

        {
            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
            oList = clientContext.Web.Lists.GetByTitle("DonXinNghiPhep");
            ListItem oListItem = oList.GetItemById(Convert.ToString(txtIdDelete.Text));
            oListItem.DeleteObject();
            //Cho phep MessageBox.Show
            clientContext.Load(oList, list => list.Title);
            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
            execute = 3;
        }

        private void btnDisplay_Click(object sender, RoutedEventArgs e)
        {
            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
            oList = clientContext.Web.Lists.GetByTitle("DonXinNghiPhep");
            CamlQuery query = new CamlQuery();           
            oListItemCollection = oList.GetItems(query);
            clientContext.Load(oListItemCollection);
            clientContext.ExecuteQueryAsync(
                new ClientRequestSucceededEventHandler(OnListItemsRequestSucceeded),
               new ClientRequestFailedEventHandler(onQueryFailed));
        }
        ListItemCollection oListItemCollection = null;
//Phương thức trả về khi thành công

        private void OnListItemsRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
        {
            // this is not called on the UI thread        

            Dispatcher.BeginInvoke(ShowListItemDetails);

        }

//Viết Class để get set từng Field

        public class DonXinNghiPhep

        {

            public int Id { getset; }

            public string Name { getset; }

            public string TuNgay { getset; }

            public string DenNgay { getset; }

        }

//Hiển thị chi tiết lên Gridview

        public void ShowListItemDetails()
        {
            List<DonXinNghiPhep> list = new List<DonXinNghiPhep>();

            foreach (ListItem i in oListItemCollection)
            {
list.Add(new DonXinNghiPhep() { Id = Convert.ToInt32(i["ID"]), Name = Convert.ToString(i["Title"]),

                TuNgay = Convert.ToString(i["TuNgay"]),

                DenNgay = Convert.ToString(i["DenNgay"])

                });
            }

            gridView.ItemsSource = list;
        }

    } 

Các bước tạo SilverLight Application và deploy vào trong SharePoint giống như ví dụ trên (HelloSilverlight)
Sau khi deploy thành công người sử dụng đăng nhập và sử dụng các chức năng như sau:
a.       Insert

b.      Update

c.       Delete

d.      Display