大家應該常用到SPQuery來進行篩選MOSS列表中的內容.
最近用到時間篩選、存在檔案夾的文件庫內容(不是列表庫)篩選就出現部分問題。下面做個筆記。
首先我們溫習下SPQuery語句,用大家都熟悉的SQL文法來和它做個比較,如下表:
| SPQuery |
SQL |
| <Where></Where> |
Where |
| <And></And> |
And |
| <Or></Or> |
Or |
| <Eq></Eq> |
= |
| <Geq></Geq> |
>= |
| <Gt></Gt> |
> |
| <Leq></Leq> |
<= |
| <Lt></Lt> |
< |
| <Neq></Neq> |
!= |
| <IsNotNull></IsNotNull> |
!=null |
| <IsNull></IsNull> |
= null |
| <Contains><Contains> |
IN |
| <BeginWith></BeginWith> |
Like '開始資訊%' |
| <OrderBy></OrderBy> |
Order By |
SPQuery中的時間篩選:
1、處理時間格式,通過SPUtility.CreateISO8601DateTimeFromSystemDateTime()將時間轉為 “yyyy-mm-ddThh:mm:ssZ”這個格式
2、進行時間比較,需要添加 IncludeTimeValue='TRUE'
SPQuery文件庫中的篩選:
添加query.ViewAttributes = "Scope='Recursive'" 遍曆到文件庫中的所有檔案夾內容
下面以尋找昨天到現在修改的檔案為例,如下:
SPList list = web.Lists[DocListName];
string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(-1)); //尋找昨天到現在修改的檔案
SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name=\"Last_x0020_Modified\"/><Value Type=\"DateTime\" IncludeTimeValue='TRUE'>" + date + "</Value></Gt></Where>";
query.ViewAttributes = "Scope='Recursive'"; //設定範圍為遞迴,包含子檔案夾
SPListItemCollection items = list.GetItems(query);