对CAML查询语句的几点小记
?CAML(Collaborative Application Markup Language)——協(xié)作應用程序標記語言,在調用WSS提供的諸多Web Service時進行數(shù)據(jù)查詢的一組XML規(guī)范,通過這種規(guī)范組織查詢語句進行數(shù)據(jù)的檢索。我在這里不想再具體介紹CAML的使用規(guī)范和具體語法了,讀者在MSDN或者園子里的其它文章中都可以了解到。
http://msdn.microsoft.com/zh-cn/library/ms462365.aspx
http://www.cnblogs.com/shanqian/archive/2008/09/25/859513.html
??? 這里我想給出我在項目實際應用中所積累的有關使用CAML查詢語句時所解決的問題。
1. Sharepoint的List中允許創(chuàng)建Document和Folder兩種類型的數(shù)據(jù),在查詢時如何查出非Folder類型的數(shù)據(jù)?
1?<Query>?2???<Where>?
3??????<Neq>?
4????????<FieldRef?Name="ContentType"/>?
5????????<Value?Type="Text">Folder</Value>?
6??????</Neq>?
7???</Where>?
8?</Query>
??? 當服務器上安裝有WSS的多語言包時,根據(jù)不同的語言ContentType的值也會有不同的寫法,可以借助CAML工具或者在WSS查詢返回的報文中查看一下,確認ContentType的具體類型名稱。
2. 如何查詢List中Lookup類型的字段的值?
??? Loopup類型的字段在數(shù)據(jù)庫中相當于外鍵,數(shù)據(jù)格式如1;#title1;#2;#title2,查詢時可以按照下面的CAML。
1?<Query>??2?<Where>??
3???<Eq>??
4????<FieldRef?Name="ProjectID"?/>??
5????<Value?Type="Lookup">My Test Project 1</Value>??
6???</Eq>??
7?</Where>??
8?</Query> ??????? Value中只需要填寫外鍵所對應的名稱,而不用填寫外鍵所對應的id。如果只知道id而不知道名稱則可以使用下面的CAML。 1?<Query>??
2?????<Where>??
3?????<Eq>??
4?????????<FieldRef?Name="ProjectID"?LookupId="TRUE"?/>??
5?????????<Value?Type="Text">3</Value>??
6?????</Eq>??
7?????</Where>??
8?</Query>
3.?Lists.GetListItems()方法中的參數(shù)。
??? 對于該方法中參數(shù)的詳解,讀者可以參考MSDN:
http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistitems.aspx
??? 這里對參數(shù)的幾點用法做一些說明:
| listName | 要查詢的List的名稱,也可以為List的GUID,不能為空。 |
| viewName | 一般都為空。 |
| query | 標準CAML查詢語句,可以為空,<Query></Query> |
| viewFields | 要查詢的字段,可以為空,<ViewFields></ViewFields> |
| rowLimit | 返回的記錄條數(shù),默認為100,如果不需要限制,將值設為0 。 |
| queryOptions | 查詢選項設置,所涉及到的選項比較多,可以參考MSDN。? http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistitems.aspx? 如果要查詢List中Folder下的文檔,則必須添加遞歸選項。? <QueryOptions>? ?? <ViewAttributes Scope="Recursive" />? </QueryOptions> |
| webID | 可選參數(shù)。具體用法查看MSDN。 |
??? List的GUID可以通過調用Lists.GetListAndView()方法,在返回的報文中獲取到。
http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistandview.aspx
4.?搜集到的List中字段的屬性,包括字段的Type。
| ID | The ID is a globally unique identifier (GUID) which you should generate for each field. This ID will be used in lists when referencing the site column | |
| Type | The type indicates the data type of the column and can be of the any following: | |
| AllDayEvent | The all day event flag is used in calendar lists. | |
| Attachments | The URL of an attachment. | |
| Boolean | A boolean indicator with Yes/No. | |
| Calculated | Indicates that the column is a calculated column based on a expression. | |
| Choice | Indicates that the column is choice from a list of items | |
| Computed | Indicates the field's value is dependant on another field's value. | |
| ContentTypeId | A Content Type ID value. | |
| Counter | An internal unique ID's / counter for each item. | |
| Currency | A currency value (its format depends on the locale). | |
| DateTime | A Date and Time field. | |
| File | A file object when used in document libraries. | |
| GridChoice | A rating scale as used in surveys | |
| Guid | A globally unique identifier. | |
| Integer | An integer number field. | |
| Lookup | The field is a lookup which is a choice, however the choice is from another list. | |
| LookupMulti | A lookup field, however multiple selections are allowed. | |
| ModStat | An approval status. | |
| MultiChoice | A choice field, however multiple selections are allowed | |
| Note | Multiple lines of text which can be plain or formatted. | |
| Number | A numerical field which allows decimal places. | |
| PageSeparator | A page separator field, for surveys. | |
| Recurrence | An indicator that identifies the field as a reoccurring calendar event. | |
| Text | A single line of text | |
| ThreadIndex | The ID of a discussion thread. | |
| Threading | Indicates the field supports threading (in discussions). | |
| URL | A Unified Resource Locator (URL) is stored. | |
| User | A person or group. | |
| UserMulti | A person or group, however multiple people or groups can be selected. | |
| WorkflowEventType | Contains the type of workflow history event (used in workflow history list). | |
| WorkflowStatus | The status of a workflow is stored. | |
| Title | The name of the field as displayed in the user interface.? This title may be set using a resource file. | |
| Name | (optional) The name of the field which should be guaranteed to never change.? The default is the Title with spaces and invalid characters removed. | |
| StaticName | (optional) The logical name of the field which is similar to Name, however this can be programmatically changed. | |
| DisplayName | (optional) See title. | |
| Description | (optional) A description of the field which will be displayed in front ends.? The description can be placed into a resource file. | |
| Required | (optional) Indicates whether or not the field is mandatory. The default is FALSE. | |
| MaxLength | (optional) The maximum number of characters allowed. | |
| Hidden | (optional) Indicates that the field should be hidden from the interface. If TRUE, this field will not be shown on views or forms. The default is FALSE. | |
| Readonly | (optional) The field should be read-only and can be displayed and not edited.? The default is FALSE. | |
| ShowInDisplayForm | (optional) Indicates whether the field should be shown on a display form. The default is TRUE. | |
| ShowInEditForm | (optional) Indicates whether the field should be shown on an edit form. The default is TRUE. | |
| ShowInListSettings | (optional) Indicates whether the field should be shown in the list settings screen. The default is TRUE. | |
| ShowInNewForm | (optional) Indicates whether the field should be shown on a New form. The default is TRUE. | |
| ShowInVersionHistory | (optional) Indicates whether the field should be shown in the version history of an item. The default is TRUE. | |
| ShowInViewForms | (optional) Indicates whether the field should be shown on a view form. The default is TRUE. | |
| Group | (optional) A group name allowing you to group fields together. | |
5.?Customer List中的列名變化?
??? 在Customer List中創(chuàng)建一個新列時,Sharepoint同時給它賦予了內部名稱(FieldInternalName)和外部名稱(FieldName),初始狀態(tài)下內部名稱和外部名稱是相同的,當我們修改了List的列名時,外部名稱被修改了,而內部名稱則不變,為了防止在使用時出現(xiàn)找不到列的錯誤,必須在CAML中使用內部名稱作為列名??梢越柚鶦AML工具查找列的內部名稱,也可以在List的新建記錄頁面中查看源代碼,搜索顯示的列名,找到類似于下面的代碼段,FieldInternalName的值即為該列的內部名稱。
<!--?FieldName="Area"???? FieldInternalName="Title"?
??? FieldType="SPFieldText"?
-->
6.?兩個小工具,用于檢測或生成CAML,對List的管理和查看報文的具體信息。實際項目應用中會很有用哦:)
U2U Caml Query Builder 2007 v3.1.0.0 (windows version)
Sharepoint Manager 2007
??? CAML的使用過程中還是會遇到很多問題的,以后遇到再逐漸補上吧!
本文轉自Jaxu博客園博客,原文鏈接:http://www.cnblogs.com/jaxu/archive/2009/03/23/1419717.html,如需轉載請自行聯(lián)系原作者
總結
以上是生活随笔為你收集整理的对CAML查询语句的几点小记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sed 学习笔记(未完成)
- 下一篇: Silverlight与HTML双向交互