对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查询语句,可以为空,<Query></Query>
viewFields要查询的字段,可以为空,<ViewFields></ViewFields>
rowLimit返回的记录条数,默认为100,如果不需要限制,将值设为0 。
queryOptions 查询选项设置,所涉及到的选项比较多,可以参考MSDN。
http://msdn.microsoft.com/zh-cn/library/websvclists.lists.getlistitems.aspx
如果要查询List中Folder下的文档,则必须添加递归选项。
<QueryOptions>
   <ViewAttributes Scope="Recursive" />
</QueryOptions>
webID 可选参数。具体用法查看MSDN。

    List的GUID可以通过调用Lists.GetListAndView()方法,在返回的报文中获取到。

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

4. 搜集到的List中字段的属性,包括字段的Type。

IDThe 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
TypeThe type indicates the data type of the column and can be of the any following:
AllDayEventThe all day event flag is used in calendar lists.
AttachmentsThe URL of an attachment.
BooleanA boolean indicator with Yes/No.
CalculatedIndicates that the column is a calculated column based on a expression.
ChoiceIndicates that the column is choice from a list of items
ComputedIndicates the field's value is dependant on another field's value.
ContentTypeIdA Content Type ID value.
CounterAn internal unique ID's / counter for each item.
CurrencyA currency value (its format depends on the locale).
DateTimeA Date and Time field.
FileA file object when used in document libraries.
GridChoiceA rating scale as used in surveys
GuidA globally unique identifier.
IntegerAn integer number field.
LookupThe field is a lookup which is a choice, however the choice is from another list.
LookupMultiA lookup field, however multiple selections are allowed.
ModStatAn approval status.
MultiChoiceA choice field, however multiple selections are allowed
NoteMultiple lines of text which can be plain or formatted.
NumberA numerical field which allows decimal places.
PageSeparatorA page separator field, for surveys.
RecurrenceAn indicator that identifies the field as a reoccurring calendar event.
TextA single line of text
ThreadIndexThe ID of a discussion thread.
ThreadingIndicates the field supports threading (in discussions).
URLA Unified Resource Locator (URL) is stored.
UserA person or group.
UserMultiA person or group, however multiple people or groups can be selected.
WorkflowEventTypeContains the type of workflow history event (used in workflow history list).
WorkflowStatusThe status of a workflow is stored.
TitleThe 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的值即为该列的内部名称。

<!-- FieldName="Area"
    FieldInternalName="Title"
    FieldType="SPFieldText"
-->

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

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

Sharepoint Manager 2007

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

转载于:https://www.cnblogs.com/jaxu/archive/2009/03/23/1419717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值