Tìm hiểu về SPQuery

Giới thiệu:

SPQuery sử dụng ngôn ngữ Collaborative Application Markup Language (CAML). Rất phức tạp. Tuy nhiên, đối với lập trình thông thường, chúng ta chỉ cần một vài điều kiện đơn giản khi sử dụng SPQuery. Cách dễ nhất để tìm hiểu SPQuery đó là qua các ví dụ.

Đầu tiên, đó là ví dụ trên MSDN:

C#

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)

{

 

SPList oList = oWebsiteRoot.Lists[“Tasks”];

 

SPQuery oQuery = new SPQuery();

oQuery.Query = “<Where><Eq><FieldRef Name=’Status’/>” +


“<Value Type=’Text’>Completed</Value></Eq></Where>”;

SPListItemCollection collListItems = oList.GetItems(oQuery);

 


foreach (SPListItem oListItem in collListItems)

{

Response.Write(SPEncode.HtmlEncode(oListItem[“Title”].ToString()) +


“<BR>”);

}

}

 

Ví dụ này sẽ hiển thị Những mẫu tin trong list “Task” có Status bằng (cái này chưa tìm hiểu kỹ nhưng chắc là chỗ <Eq>) Completed.

Ví dụ 2

Ví dụ này sẽ lấy ra những Items có FullName giống với tên của người đăng nhập hiện tại:

1

2

3

4

5

6

7

8

9

10

11

SPWeb web = SPControl.GetContextWeb(Context);

 
 

string fullName = web.CurrentUser.Name;

 
 

SPQuery oQuery =
new SPQuery();

 
 

oQuery.Query
=


“<Where>”
+


“<Eq><FieldRef Name=’FullName’/><Value Type=’Text’>'”
+ fullName +
“‘</Value></Eq>”
+


“</Where>”
+


“<OrderBy><FieldRef Name=’StartTime’ Ascending=’FALSE’></FieldRef></OrderBy>”;

 

Ví dụ 3: Sử dụng AND

Câu lệnh query này lấy ra những Item có FullName bằng với Name của người đang đăng nhập và Status bằng Complete

1

2

3

4

5

6

7

8

9

10

11

12

13

SPWeb web = SPControl.GetContextWeb(Context);

 
 

string fullName = web.CurrentUser.Name;

 
 

SPQuery oQuery =
new SPQuery();

oQuery.Query
=


“<Where>”
+


“<And>”
+


“<Eq><FieldRef Name=’FullName’/><Value Type=’Text’>'”
+ fullName +
“‘</Value></Eq>”
+


“<Eq><FieldRef Name=’Status’/><Value Type=’Text’>Complete</Value></Eq>”
+


“</And>”
+


“</Where>”
+


“<OrderBy><FieldRef Name=’StartTime’ Ascending=’FALSE’></FieldRef></OrderBy>”;

Ví dụ 4: Điều gì sẽ xảy ra nếu chúng ta có điều kiện AND gồm 3 điều kiện nhỏ hơn

Không còn giống như 2 điều kiện trên nữa, chúng ta cần phải biến đổi một chút. Đoạn mã sau sẽ không chạy và chúng ta sẽ bị báo lỗi:

rror when run:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

SPWeb web = SPControl.GetContextWeb(Context);

 
 

string fullName = web.CurrentUser.Name;

 
 

SPQuery oQuery =
new SPQuery();

oQuery.Query
=


“<Where>”
+


“<And>”
+


“<Eq><FieldRef Name=’FullName’/><Value Type=’Text’>'”
+ fullName +
“‘</Value></Eq>”
+


“<Eq><FieldRef Name=’Status’/><Value Type=’Text’>Complete</Value></Eq>”
+


“<Eq><FieldRef Name=’Manager’/><Value Type=’Text’>James Lane</Value></Eq>”
+


“</And>”
+


“</Where>”
+


									"<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef</OrderBy>";

 

Không có sự kết hợp của 3 điều kiện đồng thời. Chúng ta phải chia nhỏ chúng:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

SPWeb web = SPControl.GetContextWeb(Context);

 
 

string fullName = web.CurrentUser.Name;

 
 

SPQuery oQuery =
new SPQuery();

oQuery.Query
=


“<Where>”
+


“<And>” +


“<And>” +

“<Eq><FieldRef Name=’FullName’/><Value Type=’Text’>'” + fullName + “‘</Value></Eq>” +

“<Eq><FieldRef Name=’Status’/><Value Type=’Text’>Complete</Value></Eq>” +

“</And>” +


“<Eq><FieldRef Name=’Manager’/><Value Type=’Text’>James Lane</Value></Eq>” +

“</And>” +


“</Where>”
+


“<OrderBy><FieldRef Name=’StartTime’ Ascending=’FALSE’></FieldRef></OrderBy>”;

Và ví dụ cuối cùng là 1 điều kiện OR nằm trong điều kiện AND:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

SPWeb web = SPControl.GetContextWeb(Context);

 
 

string fullName = web.CurrentUser.Name;

 
 

SPQuery oQuery =
new SPQuery();

oQuery.Query
=


“<Where>”
+


“<And>”
+


“<Eq><FieldRef Name=’FullName’/><Value Type=’Text’>'”
+ fullName +
“‘</Value></Eq>”
+


“<Or>”
+


“<Eq><FieldRef Name=’Status’/><Value Type=’Text’>Complete</Value></Eq>”
+


“<Eq><FieldRef Name=’Status’/><Value Type=’Text’>On Hold</Value></Eq>”
+


“</Or>”
+


“</And>”
+


“</Where>”
+


“<OrderBy><FieldRef Name=’StartTime’ Ascending=’FALSE’></FieldRef></OrderBy>”;


 

Leave a comment