一个简单的CAML检索程序
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint
Class CAMLQuery
Sub Main(ByVal args() As String)
Dim ServerName As String = args(0)
Dim SiteName As String = args(1)
Dim listName As String = args(2)
Dim viewName As String = args(3)
Dim site As SPSite = New SPSite(ServerName)
Dim web As SPWeb = site.OpenWeb(siteName)
Dim myList As SPList = web.Lists(listName)
Dim query As SPQuery = New SPQuery(myList.Views(viewName))
query.Query = "<query string>"
Dim filteredList As SPListItemCollection = myList.GetItems(query)
Dim i As SPListItem
For Each i In filteredList
System.Diagnostics.Debug.WriteLine(i("Title").ToString() + " " + i("Salary").ToString() + " " + i("StartDate").ToString())
Next
Console.ReadLine()
End Sub
End Class
有几点要特别小心
1:Field名中最好全部是字母,不要用特殊字符包括空格,因为特殊字符需要转换。
例如:“Start Date”被转换成 “Start_x0020_Date”
2: Field名一旦确定,最好不要更名,因为内部还是使用旧名。容易混淆。
3: 如果field type是hyperlink时,被检索出的结果,指向本地的url 会是相对路径,而不是绝对路径。
讲到CAML不可不提大名鼎鼎的U2U CAML Query Builder(http://www.
u2u.info/SharePoint/U2U%20Community%20Tools/Forms/AllItems.aspx.)。对开发CAML程序而言,这是一个非常有用的工具。从网上拷贝二张截图,分别是用这个工具进行检索和检索结果。