对CAML查询语句的几点小记

    CAML(Collaborative Application Markup Language)——协作应用程序标记语言,在调用WSS提供的诸多Web Service时进行数据查询的一组XML规范,通过这种规范组织查询语句进行数据的检索。我在这里不想再具体介绍CAML的使用规范和具体语法了,读者在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中允许创建Document和Folder两种类型的数据,在查询时如何查出非Folder类型的数据?

1  < Query >
2    < Where >
3       < Neq >
4         < FieldRef Name ="ContentType" />
5         < Value Type ="Text" > Folder Value >
6       Neq >
7    Where >
8  Query >

    当服务器上安装有WSS的多语言包时,根据不同的语言ContentType的值也会有不同的写法,可以借助CAML工具或者在WSS查询返回的报文中查看一下,确认ContentType的具体类型名称。

2. 如何查询List中Lookup类型的字段的值?

    Loopup类型的字段在数据库中相当于外键,数据格式如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()方法中的参数。

    对于该方法中参数的详解,读者可以参考MSDN:

http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistitems.aspx

    这里对参数的几点用法做一些说明:

listName 要查询的List的名称,也可以为List的GUID,不能为空。
viewName 一般都为空。
query 标准CAML查询语句,可以为空,
viewFields 要查询的字段,可以为空,
rowLimit 返回的记录条数,默认为100,如果不需要限制,将值设为0 。
queryOptions 查询选项设置,所涉及到的选项比较多,可以参考MSDN。
http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistitems.aspx
如果要查询List中Folder下的文档,则必须添加递归选项。

  
webID 可选参数。具体用法查看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中创建一个新列时,Sharepoint同时给它赋予了内部名称(FieldInternalName)和外部名称(FieldName),初始状态下内部名称和外部名称是相同的,当我们修改了List的列名时,外部名称被修改了,而内部名称则不变,为了防止在使用时出现找不到列的错误,必须在CAML中使用内部名称作为列名。可以借助CAML工具查找列的内部名称,也可以在List的新建记录页面中查看源代码,搜索显示的列名,找到类似于下面的代码段,FieldInternalName的值即为该列的内部名称。

6. 两个小工具,用于检测或生成CAML,对List的管理和查看报文的具体信息。实际项目应用中会很有用哦:)

U2U Caml Query Builder 2007 v3.1.0.0 (windows version)

Sharepoint Manager 2007

    CAML的使用过程中还是会遇到很多问题的,以后遇到再逐渐补上吧!

你可能感兴趣的:(对CAML查询语句的几点小记)