对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。

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的值即为该列的内部名称。

<!--  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,如需转载请自行联系原作者


相关文章
|
5月前
|
SQL 缓存 Java
sql优化方法
sql优化方法
62 0
|
6天前
|
SQL 资源调度 数据库
深入探究SQL查询语句执行过程
深入探究SQL查询语句执行过程
18 2
|
2月前
|
SQL 数据处理 数据库
SQL进阶之路:深入解析数据更新与删除技巧——掌握批量操作、条件筛选、子查询和事务处理,提升数据库维护效率与准确性
【8月更文挑战第31天】在数据库管理和应用开发中,数据的更新和删除至关重要,直接影响数据准确性、一致性和性能。本文通过具体案例,深入解析SQL中的高级更新(UPDATE)和删除(DELETE)技巧,包括批量更新、基于条件的删除以及使用子查询和事务处理复杂场景等,帮助读者提升数据处理能力。掌握这些技巧能够有效提高数据库性能并确保数据一致性。
52 0
|
4月前
|
SQL 关系型数据库 MySQL
SQL优化方法有哪些?
【6月更文挑战第16天】SQL优化方法有哪些?
248 5
|
3月前
|
SQL 存储 关系型数据库
常见的一些SQL优化方式
SQL 优化是提高数据库性能和查询效率的重要手段。
24 0
|
5月前
|
SQL 缓存 关系型数据库
一次sql改写优化子查询的案例
在生产环境中,一个MySQL RDS实例遭遇了高CPU使用率问题,原因是执行了一条复杂的UPDATE SQL语句,该语句涉及一个无法缓存的子查询(UNCACHEABLE SUBQUERY),导致子查询需要针对每一行数据重复执行,极大地影响了性能。SQL语句的目标是更新一行数据,但执行时间长达30秒。优化方法是将子查询转换为内连接形式,优化后的语句执行时间降低到毫秒级别,显著减少了CPU消耗。通过示例数据和执行计划对比,展示了优化前后的时间差异和执行效率的提升。
237 2
|
5月前
|
SQL 存储 关系型数据库
如何巧用索引优化SQL语句性能?
在 MySQL 中,添加合适的索引可以显著提升慢查询的速度,因为索引加快了数据检索。要优化 SQL 性能,首先需定位慢查询,可通过查看执行时间和执行计划。`EXPLAIN` 命令用于查看执行计划,分析如`type`(全表扫描最慢,索引扫描较快)、`key`(未使用索引为NULL)等字段。例如,全表扫描的查询可考虑为慢查询,并创建相应索引进行优化。此外,注意聚簇索引、索引覆盖和最左前缀原则等索引使用技巧,以提高查询效率。启用慢查询日志并设置阈值,有助于识别已运行的慢查询。
112 0
|
SQL 关系型数据库 MySQL
干货总结-全面解析SQL优化
干货总结-全面解析SQL优化
|
SQL Oracle 关系型数据库
【SQL开发实战技巧】系列(五):从执行计划看IN、EXISTS 和 INNER JOIN效率,我们要分场景不要死记网上结论
从执行计划角度分析IN、EXISTS 和 INNER JOIN效率而不是死记网上结论、表的5种关联:INNER JOIN、LEFT JOIN、RIGHT JOIN 和 FULL JOIN 解析【SQL开发实战技巧】这一系列博主当作复习旧知识来进行写作,毕竟SQL开发在数据分析场景非常重要且基础,面试也会经常问SQL开发和调优经验,相信当我写完这一系列文章,也能再有所收获,未来面对SQL面试也能游刃有余~。
【SQL开发实战技巧】系列(五):从执行计划看IN、EXISTS 和 INNER JOIN效率,我们要分场景不要死记网上结论
|
SQL 关系型数据库 MySQL
SQL优化场景方式(下)
SQL优化场景方式(下)
下一篇
无影云桌面