35
Bài 5: Quản lý dữ liệu SharePoint

Bài 5: Quản lý dữ liệu SharePoint

Embed Size (px)

DESCRIPTION

Tìm hiểu Events (sự kiện) trong SharePoint 2010 Truy vấn dữ liệu sử dụng CAML Truy vấn dữ liệu sử dụng LINQ to SharePoint

Citation preview

Page 1: Bài 5: Quản lý dữ liệu SharePoint

Bài 5:Quản lý dữ liệu SharePoint

Page 2: Bài 5: Quản lý dữ liệu SharePoint

Hệ thống bài cũ

Tìm hiểu ADO.NET Data Services và REST trênSharePointTìm hiểu các thủ tục thao tác dữ liệu SharePoint sử dụngADO.NET Data Services

Quản lý dữ liệu SharePoint 2

Page 3: Bài 5: Quản lý dữ liệu SharePoint

Nội dung bài học

Tìm hiểu Events (sự kiện) trong SharePoint 2010Truy vấn dữ liệu sử dụng CAMLTruy vấn dữ liệu sử dụng LINQ to SharePoint

Quản lý dữ liệu SharePoint 3

Page 4: Bài 5: Quản lý dữ liệu SharePoint

Sự kiện trong SharePoint

Sự kiện (Event) trong SharePoint là Microsoft .NETassembly chứa một hoặc nhiều lớp xử lý sự kiện xảy ratrên SharePoint

Ngăn chặn việc xóa côngviệc khi chưa hoàn

thành

Quản lý dữ liệu SharePoint 4

Giám sát mọi thay đổiliên quan đến công việc

Điều chỉnh quy trình xửlý khi một công việc cóđộ ưu tiên thay đổi đến

mức cao

Ngăn chặn việc xóa mộttrường dữ liệu khỏi List

Page 5: Bài 5: Quản lý dữ liệu SharePoint

Before Event (Synchronous)

Được đặt tên với kết thúc là …ingVd: ItemAdding

Sự kiện này xảy ra trước khi sự kiện trên SharePointthực thi và trước khi SharePoint lưu dữ liệu xuống CSDLHỗ trợ cơ chế từ chối thực thi thông qua e.Cancel = trueThường dùng để phê chuẩn dữ liệu trên SharePoint

Được đặt tên với kết thúc là …ingVd: ItemAdding

Sự kiện này xảy ra trước khi sự kiện trên SharePointthực thi và trước khi SharePoint lưu dữ liệu xuống CSDLHỗ trợ cơ chế từ chối thực thi thông qua e.Cancel = trueThường dùng để phê chuẩn dữ liệu trên SharePoint

Quản lý dữ liệu SharePoint 5

Page 6: Bài 5: Quản lý dữ liệu SharePoint

Before Event (Synchronous)

Synchronous (đồng bộ) Event:Là sự kiện trên SharePoint mà được thực thi ngay khi vàtrong cùng một thread (luồng hay tiến trình) với hànhđộng gây nên sự kiện đóToàn bộ Before event là Synchronous event.

Quản lý dữ liệu SharePoint 6

Page 7: Bài 5: Quản lý dữ liệu SharePoint

After Event (Asynchronous)

Được đặt tên với kết thúc là …edVd: ItemAdded

Sự kiện này xảy ra sau khi sự kiện trên SharePoint đãthực thi và dữ liệu đã được ghi xuống CSDLKhông hỗ trợ cơ chế từ chối sự kiệnViệc thực thi sự kiện này là không đồng bộ

Được đặt tên với kết thúc là …edVd: ItemAdded

Sự kiện này xảy ra sau khi sự kiện trên SharePoint đãthực thi và dữ liệu đã được ghi xuống CSDLKhông hỗ trợ cơ chế từ chối sự kiệnViệc thực thi sự kiện này là không đồng bộ

Quản lý dữ liệu SharePoint 7

Page 8: Bài 5: Quản lý dữ liệu SharePoint

After Event (Asynchronous)

Asynchronous (không đồng bộ) EventLà sự kiện trên SharePoint mà được thực thi trên mộtthread (luồng hay tiến trình) khác với hành động gây nênsự kiện đó và việc thực thi này có thể xảy ra sau một thờigian so với hành động gây nên sự kiện.

Asynchronous (không đồng bộ) EventLà sự kiện trên SharePoint mà được thực thi trên mộtthread (luồng hay tiến trình) khác với hành động gây nênsự kiện đó và việc thực thi này có thể xảy ra sau một thờigian so với hành động gây nên sự kiện.

Quản lý dữ liệu SharePoint 8

Page 9: Bài 5: Quản lý dữ liệu SharePoint

Sự kiện trong SharePoint 2010

Event Host: Các đối tượng trên SharePoint có thể viết sựkiện do người dùng định nghĩa bao gồm

Site Collections:Bao gồm các sự kiện: Xóa site collection

Sites:Bao gồm các sự kiện: Tạo, xóa, thay đổi URL của site…

Lists:Bao gồm các sự kiện: Tạo, xóa, nhận email…của List haytạo, sửa, xóa, check-in, check-out…của List item

Workflows:Bao gồm các sự kiện: Khởi động, hoàn thành, tạm dừngworkflow…

Event Host: Các đối tượng trên SharePoint có thể viết sựkiện do người dùng định nghĩa bao gồm

Site Collections:Bao gồm các sự kiện: Xóa site collection

Sites:Bao gồm các sự kiện: Tạo, xóa, thay đổi URL của site…

Lists:Bao gồm các sự kiện: Tạo, xóa, nhận email…của List haytạo, sửa, xóa, check-in, check-out…của List item

Workflows:Bao gồm các sự kiện: Khởi động, hoàn thành, tạm dừngworkflow…

Quản lý dữ liệu SharePoint 9

Page 10: Bài 5: Quản lý dữ liệu SharePoint

Các loại sự kiện trongSharePoint 2010

List Events:Các sự kiện xảy ra cho List:

List Item Events:Các sự kiện xảy ra đối với item trong list hay documenttrong document library

List Email EventsCác sẽ kiện xảy ra đối với quá trình nhận email của list

Web EventsCác sự kiện xảy ra đối với site

List Workflow EventsCác sự kiện xảy ra đối với Workflow

List Events:Các sự kiện xảy ra cho List:

List Item Events:Các sự kiện xảy ra đối với item trong list hay documenttrong document library

List Email EventsCác sẽ kiện xảy ra đối với quá trình nhận email của list

Web EventsCác sự kiện xảy ra đối với site

List Workflow EventsCác sự kiện xảy ra đối với Workflow

Quản lý dữ liệu SharePoint 10

Tham khảo: http://msdn.microsoft.com/en-us/library/gg749858.aspx

Page 11: Bài 5: Quản lý dữ liệu SharePoint

Sự kiện trong SharePoint 2010

Lớp Event receiver Host Events được hỗ trợSPWebEventReceiver SPSite, SPWeb SiteDeleting, SiteDeleted,

WebAdding, WebProvisioned,WebDeleting, WebDeleted,WebMoving, WebMoved,

SPListEventReceiver(lists)

SPSite, SPWeb ListAdding, ListAdded,ListDeleting, ListDeleted

Quản lý dữ liệu SharePoint 11

SPListEventReceiver(lists)

SPSite, SPWeb ListAdding, ListAdded,ListDeleting, ListDeleted

SPListEventReceiver(fields)

SPSite, SPWeb,SPList,SPContentType

FieldAdding, FieldAdded,FieldDeleting, FieldDeleted,FieldUpdating, FieldUpdated,

SPEmailEventReceiver SPSite, SPWeb,SPList

EmailReceived,

Tham khảo: http://msdn.microsoft.com/en-us/library/gg749858.aspx

Page 12: Bài 5: Quản lý dữ liệu SharePoint

Sự kiện trong SharePoint 2010

Lớp Eventreceiver

Host Events được hỗ trợ

SPItemEventReceiver

SPSite, SPWeb,SPList,SPContentType

ItemAdding, ItemAdded, ItemDeleting,ItemDeleted, ItemUpdating, ItemUpdated,ItemFileConverted, ItemFileMoving,ItemFileMoved, ItemCheckingIn,ItemCheckedIn, ItemCheckingOut,ItemCheckedOut, ItemAttachmentAdding,ItemAttachmentAdded,ItemAttachmentDeleting,ItemAttachmentDeleted

Quản lý dữ liệu SharePoint 12

ItemAdding, ItemAdded, ItemDeleting,ItemDeleted, ItemUpdating, ItemUpdated,ItemFileConverted, ItemFileMoving,ItemFileMoved, ItemCheckingIn,ItemCheckedIn, ItemCheckingOut,ItemCheckedOut, ItemAttachmentAdding,ItemAttachmentAdded,ItemAttachmentDeleting,ItemAttachmentDeleted

SPWorkflowEventReceiver

SPSite, SPWeb,SPList,SPContentType

WorkflowStarting, WorkflowStarted,WorkflowCompleted,WorkflowPostponed,

Page 13: Bài 5: Quản lý dữ liệu SharePoint

Khai báo sự kiện trong Element.xml

<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Receivers ListTemplateId="104">

<Receiver><Name>EventReceiver1ItemAdding</Name><Type>ItemAdding</Type><Assembly>$SharePoint.Project.AssemblyFullName$</Assembly><Class>Lab05.EventReceiver.EventReceiver1</Class><SequenceNumber>10000</SequenceNumber>

</Receiver></Receivers>

</Elements>

Quản lý dữ liệu SharePoint 13

<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Receivers ListTemplateId="104">

<Receiver><Name>EventReceiver1ItemAdding</Name><Type>ItemAdding</Type><Assembly>$SharePoint.Project.AssemblyFullName$</Assembly><Class>Lab05.EventReceiver.EventReceiver1</Class><SequenceNumber>10000</SequenceNumber>

</Receiver></Receivers>

</Elements>

Page 14: Bài 5: Quản lý dữ liệu SharePoint

Receivers element

Attribute Mô tảListTemplateId Chỉ ra Event sẽ áp dụng cho loại List nào

ListTemplateOwner Tùy chọn

ListUrl Chỉ ra URL của list mà Event sẽ áp dụngcho

Quản lý dữ liệu SharePoint 14

Chỉ ra URL của list mà Event sẽ áp dụngcho

RootWebOnly Tùy chọn kiểu Boolean. True nếu Eventchỉ áp dụng cho Root Web

Scope Phạm vi triển khai Event. Có thể là- Site: Áp dụng cho toàn Site Collection- Web: Chỉ áp dụng cho Web site

Tham khảo: http://msdn.microsoft.com/en-us/library/ms431081.aspx

Page 15: Bài 5: Quản lý dữ liệu SharePoint

SharePoint Events trong VS2010

Quản lý dữ liệu SharePoint 15

Page 16: Bài 5: Quản lý dữ liệu SharePoint

DEMO

Demo:Viết một Event Receiver sử dụng Visual Studio 2010

Quản lý dữ liệu SharePoint 16

Page 17: Bài 5: Quản lý dữ liệu SharePoint

Truy vấn CAML

CAML (Collaborative Application Markup Language): làmột ngôn ngữ dựa trên XML được sử dụng trongSharePoint để định nghĩa các trường dữ liệu (fields) haycác View được sử dụng trên SharePoint List và SiteCAML cũng được sử dụng để truy vấn dữ liệu trênSharePoint

CAML (Collaborative Application Markup Language): làmột ngôn ngữ dựa trên XML được sử dụng trongSharePoint để định nghĩa các trường dữ liệu (fields) haycác View được sử dụng trên SharePoint List và SiteCAML cũng được sử dụng để truy vấn dữ liệu trênSharePoint

Quản lý dữ liệu SharePoint 17

Page 18: Bài 5: Quản lý dữ liệu SharePoint

Các thành phần trongtruy vấn CAML

Thành phần Mô tảAnd Nhóm nhiều điều kiện

BeginsWith Tìm kiếm chuỗi ký tự mà bắt đầu với

Contains Tìm kiếm chuỗi có chứa

Eq Toán từ so sánh bằng

FieldRef Tham chiếu tới một FieldFieldRef Tham chiếu tới một Field

Geq Lớn hơn hoặc bằng

GroupBy Nhóm kết quả nhận được

Gt Lớn hơn

IsNotNull Không được rỗng

IsNull Rỗng

Quản lý dữ liệu SharePoint 18

Tham khảo: http://msdn.microsoft.com/en-us/library/ms467521.aspx

Page 19: Bài 5: Quản lý dữ liệu SharePoint

Các thành phần trongtruy vấn CAML

Thành phần Mô tảLeq Nhỏ hơn hoặc bằng

Lt Nhỏ hơn

Neq Không bằng

Now Thời gian hiện tại

Quản lý dữ liệu SharePoint 19

Now Thời gian hiện tại

Or Hoặc

OrderBy Sắp xếp kết quảToday Ngày hiện tại

TodayIso Ngày hiện tại với định dạng ISO

Where Lọc

Tham khảo: http://msdn.microsoft.com/en-us/library/ms467521.aspx

Page 20: Bài 5: Quản lý dữ liệu SharePoint

Ví dụ truy vấn CAML

<Query><OrderBy>

<FieldRef Name="Modified" Ascending="FALSE"></FieldRef></OrderBy><Where>

<Or><Neq>

<FieldRef Name="Status"></FieldRef><Value Type="Text">Completed</Value>

</Neq><IsNull>

<FieldRef Name="Status"></FieldRef></IsNull>

</Or></Where>

</Query>

<Query><OrderBy>

<FieldRef Name="Modified" Ascending="FALSE"></FieldRef></OrderBy><Where>

<Or><Neq>

<FieldRef Name="Status"></FieldRef><Value Type="Text">Completed</Value>

</Neq><IsNull>

<FieldRef Name="Status"></FieldRef></IsNull>

</Or></Where>

</Query>

Quản lý dữ liệu SharePoint 20

<Query><OrderBy>

<FieldRef Name="Modified" Ascending="FALSE"></FieldRef></OrderBy><Where>

<Or><Neq>

<FieldRef Name="Status"></FieldRef><Value Type="Text">Completed</Value>

</Neq><IsNull>

<FieldRef Name="Status"></FieldRef></IsNull>

</Or></Where>

</Query>

<Query><OrderBy>

<FieldRef Name="Modified" Ascending="FALSE"></FieldRef></OrderBy><Where>

<Or><Neq>

<FieldRef Name="Status"></FieldRef><Value Type="Text">Completed</Value>

</Neq><IsNull>

<FieldRef Name="Status"></FieldRef></IsNull>

</Or></Where>

</Query>

Page 21: Bài 5: Quản lý dữ liệu SharePoint

DEMO

Demo:Ứng dụng Console truy vấn dữ liệu SharePoint sử dụngCAML

Quản lý dữ liệu SharePoint 21

Page 22: Bài 5: Quản lý dữ liệu SharePoint

Truy vấn LINQ to SharePoint

LINQ to SharePoint Provider được định nghĩa trongnamespace Microsoft.SharePoint.Linq.SharePoint thực hiện việc chuyển truy vấn dưới dạngLINQ sang truy vấn dưới dạng CAML trước khi thực thi.LINQ to SharePoint là một cách thức để truy vấn dữ liệuSharePoint mà không phải sử dụng CAML.

LINQ to SharePoint Provider được định nghĩa trongnamespace Microsoft.SharePoint.Linq.SharePoint thực hiện việc chuyển truy vấn dưới dạngLINQ sang truy vấn dưới dạng CAML trước khi thực thi.LINQ to SharePoint là một cách thức để truy vấn dữ liệuSharePoint mà không phải sử dụng CAML.

Quản lý dữ liệu SharePoint 22

Page 23: Bài 5: Quản lý dữ liệu SharePoint

Truy vấn LINQ to SharePoint

Xây dựng dựa trên nền tảng và cú pháp LINQ (tương tựnhư LINQ to SQL hay LINQ to Object trong Microsoft.NET framework), dành riêng để truy vấn dữ liệuSharePointChỉ hoạt động cho các ứng dụng Server-side (phíaserver)

Để truy vấn LINQ dữ liệu SharePoint từ phía Client sửdụng ADO.NET Data Services

Xây dựng dựa trên nền tảng và cú pháp LINQ (tương tựnhư LINQ to SQL hay LINQ to Object trong Microsoft.NET framework), dành riêng để truy vấn dữ liệuSharePointChỉ hoạt động cho các ứng dụng Server-side (phíaserver)

Để truy vấn LINQ dữ liệu SharePoint từ phía Client sửdụng ADO.NET Data Services

Quản lý dữ liệu SharePoint 23

Page 24: Bài 5: Quản lý dữ liệu SharePoint

Sử dụng LINQ to SharePoint

Tạo các lớpthực thể

TạoDataContext

Viết truyvấn

Quản lý dữ liệu SharePoint 24

Tạo các lớpthực thể

TạoDataContext

Viết truyvấn

Page 25: Bài 5: Quản lý dữ liệu SharePoint

Tạo các lớp thực thể

Tự động tạo các lớp thực thể tương ứng với cácLists/Libraries trên SharePoint sử dụng công cụSPMetal.exe

spmetal /web:<url> /code:Projects.csTự động tạo các lớp cho một hoặc nhiều ListTự động tạo đối tượng DataContext

Tự động tạo các lớp thực thể tương ứng với cácLists/Libraries trên SharePoint sử dụng công cụSPMetal.exe

spmetal /web:<url> /code:Projects.csTự động tạo các lớp cho một hoặc nhiều ListTự động tạo đối tượng DataContext

Quản lý dữ liệu SharePoint 25

Page 26: Bài 5: Quản lý dữ liệu SharePoint

Tạo các lớp thực thể

Add Reference tới Microsoft.SharePoint.Linq.dllThêm mã nguồn của Entity được sinh ra từ công cụSPMetal vào Visual Studio Project

[ContentTypeAttribute(Name="Announcement", Id="0x0104")]public partial class Announcement : Item {[ColumnAttribute(Name="Body", Storage="_body", FieldType="Note")]public string Body {

get { return this._body; }set {

if ((value != this._body)) {this.OnPropertyChanging("Body", this._body);this._body = value; this.OnPropertyChanged("Body");}

}}

Quản lý dữ liệu SharePoint 26

[ContentTypeAttribute(Name="Announcement", Id="0x0104")]public partial class Announcement : Item {[ColumnAttribute(Name="Body", Storage="_body", FieldType="Note")]public string Body {

get { return this._body; }set {

if ((value != this._body)) {this.OnPropertyChanging("Body", this._body);this._body = value; this.OnPropertyChanged("Body");}

}}

Page 27: Bài 5: Quản lý dữ liệu SharePoint

Tạo đối tượng DataContext

Sử dụng các lớp EntitiesSử dụng đối tượng DataContext để truy cập tới cácEntity

public partial class Lab05DataContext : DataContext {…public Lab05DataContext(string requestUrl): base(requestUrl){

this.OnCreated();}…

}

Quản lý dữ liệu SharePoint 27

public partial class Lab05DataContext : DataContext {…public Lab05DataContext(string requestUrl): base(requestUrl){

this.OnCreated();}…

}

Page 28: Bài 5: Quản lý dữ liệu SharePoint

List có quan hệ

Có thể thực hiện truy vấn chéo giữa các List có quan hệSử dụng thuộc tính Association để khai báo quan hệ giữacác list (Entity)

[ContentTypeAttribute(Name="Employees", Id="0x0104")]public partial class Employees : Item {[AssociationAttribute(Name="Body",

Storage="_project",MultivalueType=AssociationType.SingleList=“Projects")]

public string Project {get {…}set {…}}

Quản lý dữ liệu SharePoint 28

[ContentTypeAttribute(Name="Employees", Id="0x0104")]public partial class Employees : Item {[AssociationAttribute(Name="Body",

Storage="_project",MultivalueType=AssociationType.SingleList=“Projects")]

public string Project {get {…}set {…}}

Page 29: Bài 5: Quản lý dữ liệu SharePoint

List có quan hệ

Truy vấn chéo giữa các list có quan hệ sử dụng cú phápLINQ

Ví dụ: List Employees có quan hệ với List Project

var qw = from emp in dc.Employeeswhere emp.Project.DueDate < DateTime.Now.AddYears(5)orderby emp.Project.DueDateselect emp;

Quản lý dữ liệu SharePoint 29

var qw = from emp in dc.Employeeswhere emp.Project.DueDate < DateTime.Now.AddYears(5)orderby emp.Project.DueDateselect emp;

Page 30: Bài 5: Quản lý dữ liệu SharePoint

Sửa đổi dữ liệu

Các thay đổi đối với các đối tượng Entity (List item…)được kiểm soát bởi LINQ Provider.Để thực hiện thay đổi, gọi phương thứcSubmitChanges()

Quản lý dữ liệu SharePoint 30

Page 31: Bài 5: Quản lý dữ liệu SharePoint

Sửa đổi dữ liệu

// Khởi tạo đối tượng DataContextDataContext data = new DataContext(SPContext.Current.Web.Url);

// Lấy ra đối tượng Customers listEntityList<Customer> Customers

= data.GetList<Customer>("Customers");

// Tạo mới một đối tượng CustomerCustomer newCustomer = new Customer()

{ CustomerId=36, City="Madrid" };

// Đánh dấu đối tượng này sẽ được thêm vào trong lần Submit tớiCustomers.InsertOnSubmit(newCustomer);

// Submit toàn bộ thay đổidata.SubmitChanges();

Quản lý dữ liệu SharePoint 31

// Khởi tạo đối tượng DataContextDataContext data = new DataContext(SPContext.Current.Web.Url);

// Lấy ra đối tượng Customers listEntityList<Customer> Customers

= data.GetList<Customer>("Customers");

// Tạo mới một đối tượng CustomerCustomer newCustomer = new Customer()

{ CustomerId=36, City="Madrid" };

// Đánh dấu đối tượng này sẽ được thêm vào trong lần Submit tớiCustomers.InsertOnSubmit(newCustomer);

// Submit toàn bộ thay đổidata.SubmitChanges();

Page 32: Bài 5: Quản lý dữ liệu SharePoint

DEMO

Demo:Ứng dụng Console sử dụng LINQ to SharePoint Providerđể truy vấn dữ liệu SharePoint

Quản lý dữ liệu SharePoint 32

Page 33: Bài 5: Quản lý dữ liệu SharePoint

Tổng kết bài học

Chúng ta có thể viết các Event để xử lý các sự kiện đượcthực thi trên SharePointCó hai loại Event trên SharePoint bao gồm

Before eventAfter event

CAML được sử dụng như là ngôn ngữ truy vấn dữ liệuSharePoint

Chúng ta có thể viết các Event để xử lý các sự kiện đượcthực thi trên SharePointCó hai loại Event trên SharePoint bao gồm

Before eventAfter event

CAML được sử dụng như là ngôn ngữ truy vấn dữ liệuSharePoint

Quản lý dữ liệu SharePoint 33

Page 34: Bài 5: Quản lý dữ liệu SharePoint

Tổng kết bài học

Ngoài việc sử dụng CAML chúng ta có thể sử dụng LINQto SharePoint để truy vấn dữ liệu SharePoint trên môitrường server.DataContext là đối tượng trung tâm trong LINQ toSharePoint để truy vấn dữ liệu

Quản lý dữ liệu SharePoint 34

Page 35: Bài 5: Quản lý dữ liệu SharePoint

Tham khảo

http://msdn.microsoft.com/en-us/library/gg749858.aspxhttp://msdn.microsoft.com/en-us/library/ee535491.aspx

Quản lý dữ liệu SharePoint 35