SharePoint CAML 查询到 T-SQL
我希望将 CAML 查询转换为 T-SQL。对于我的 SharePoint 站点,我还有一个数据仓库 (SQL Server 2005),它托管相同的数据(通过 SPList EventReceivers 填充:ItemAdded、ItemUpdated)。
我参与了一项处理列表增长和显示列表数据的自定义 Web 部件速度的任务。我希望使用数据仓库显示我们的列表内容,然后我们将从工作流程已完成的 SPList 中删除项目。
目前,显示 SPList 内容的自定义 Web 部件的用户可以选择 SPView 以按照他们想要的方式显示内容(即过滤、排序和仅显示必要的列)。我想保留此功能,并希望获取视图的 CAML 查询并将其转换为 T-SQL 以对数据仓库进行查询。
例如
<Query>
<Where>
<Or>
<Eq>
<FieldRef Name="ContentType" />
<Value Type="Text">My Content Type</Value>
</Eq>
<IsNotNull>
<FieldRef Name="Description" />
</IsNotNull>
</Or>
</Where>
<Order>
<FieldRef Name="Author" />
<FieldRef Name="AuthoringDate" />
<FieldRef Name="AssignedTo" Ascending="True" />
</Order>
<Group>
<FieldRef Name="Title" />
</Group>
</Query>
到
WHERE ContentType="My Content Type"
OR Description<>null
GROUPBY Title DESC
ORDERBY Author, AuthoringDate, AssignedTo ASC
有谁知道该怎么做?或者有可以解决这个问题的替代建议吗?我找到了多种将 T-SQL 转换为 CAML 查询的解决方案,而不是相反(即 http://yacamlqt. codeplex.com/ -- 这也是我从中检索示例的地方)
谢谢!
I was hoping to do convert a CAML query into T-SQL. For my SharePoint site, I also have a data warehouse (SQL Server 2005) which hosts the same data (it is populated through SPList EventReceivers: ItemAdded, ItemUpdated).
I am involved in a task to handle the growth of the lists and the speed of custom webparts that display list data. I am hoping instead to display our list contents using the data warehouse and then we are going to remove items from the SPLists where the workflow has been completed.
Currently, the user of the custom webpart that displays the SPList contents is allowed to select an SPView to display the content in the way they would like (i.e. filtering, sorting and only showing necessary columns). I would like to keep this functionality and was hoping to take the view's CAML query and convert it to T-SQL to query against the data warehouse.
e.g.
<Query>
<Where>
<Or>
<Eq>
<FieldRef Name="ContentType" />
<Value Type="Text">My Content Type</Value>
</Eq>
<IsNotNull>
<FieldRef Name="Description" />
</IsNotNull>
</Or>
</Where>
<Order>
<FieldRef Name="Author" />
<FieldRef Name="AuthoringDate" />
<FieldRef Name="AssignedTo" Ascending="True" />
</Order>
<Group>
<FieldRef Name="Title" />
</Group>
</Query>
to
WHERE ContentType="My Content Type"
OR Description<>null
GROUPBY Title DESC
ORDERBY Author, AuthoringDate, AssignedTo ASC
Does anyone know how to do this? Or have an alternative suggestion that would solve this issue? I have found multiple solutions for converting T-SQL to CAML query, just not the other way around (i.e. http://yacamlqt.codeplex.com/ -- which is also where I retrieved the example from)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我还希望找到一段代码将 CAML 转换为 SQL,以便构建我自己的 SQL 语句来访问数据。
我的主要项目是构建一个 SharePoint 功能区扩展,以将列表(内部和外部)内容导出到 CSV,并且在外部列表的情况下,能够绕过外部内容类型 (BCS) 中施加的限制 (2000)。
我使用元数据存储和安全存储中的信息来构建连接字符串并直接访问数据库。
当我需要改进代码以包含过滤器时,我最终构建了自己的方法来获取视图查询的“Where”部分并将其转换为类似 SQL 的Where 语句:
输入:
In我的例子是一个 SPView 对象,但它可以很容易地转换为使用字符串。我正在从中提取类似“CAML”的查询。
输出:
以下是方法:
此方法将从视图查询中提取“Where”节点,并
将其传递给一个方法来处理它并返回类似 SQL 的语句。
该方法将调用另一个方法来递归地遍历所有节点以获取视图查询“Where”节点内的值和运算符。它将在“OR”语句周围加上圆括号以保留操作优先级。
此方法完成了大部分工作来迭代每个节点:
最后一个方法可能包含在递归方法中,但在我构建代码的第一次迭代中,制作一个单独的方法更有意义,我保持这种方式。
它只是获取有关运算符的一些信息并关联一个运算符字符串,该字符串将用于构造 SQLWhere 语句的各个部分。
我知道它不是一个完整的转换工具,但它是一个开始,目前它满足我的需要。我希望这会对某人有所帮助并节省他们一些宝贵的时间。
I was also hoping to find a piece of code to convert CAML to SQL in order to build my own SQL Statement to access the data.
My main project is to build a SharePoint ribbon extension to export Lists (Internal and External) content to CSV and in the case of external lists, to be able to bypass the throttling limit (2000) imposed in External Content Types (BCS).
I'm using the information in the metada store and the secure store to build the connection string and access the Database directly.
When I needed to improve my code to include filters, I ended up building my own methods to get the "Where" part of a View Query and convert it to a SQL-Like Where statement:
Input:
In my case it's a SPView object but it can easily be converted to use a string instead. I'm extracting the "CAML" like query from it.
Ouput:
Here are the methods:
This method will extract the "Where" node from a view Query and
pass it to a method to process it and return a SQL like statement.
This method will call another method to recursively go through all the nodes to get the values and operators within the View Query "Where" node. It will put round bracket around "OR" statements to conserve the operation priority.
This method does most of the work to go throught an iterate each nodes:
This last methods could probably been included in the recursive one but in my first iteration of building the code it made more sense to make a separate one and I kept it this way.
It simply gets some information on the operators and associates an operator string which will be used to construct the individual pieces of the SQL Where Statement.
I know it's not a full conversion tool but it's a start and for now it fits my need. I hope this will help someone and save them some valuable time.
这在技术上不是一个答案,但我觉得有必要。你的做法有点倒退了。在我看来,您真正想做的是使用 sharepoint 作为数据仓库的 UI。如果是这种情况,我会将您的方法切换到本问题范围之外的几个选项之一。
Microsoft 仅通过对象模型、Web 服务或用户界面支持 Sharepoint 中的数据访问。在此之外的任何交互都可能导致大量不受支持的模式,包括损坏的数据、数据锁定、变化的结果集、安全访问等……
听起来真正需要的是 BDC 服务。这将允许您使用数据仓库进行中央存储,并为您提供列表交互的共享点本机功能。
我不确定您使用的是哪个版本,但如果您真的想直接访问数据,SQL 社区中有大量关于直接访问数据的文章。还有一个用于 LINQtoSharePoint 的 codeplex 项目 http://linqtosharepoint.codeplex.com/ 您可以将其用于对象支持您可以使用 LINQ 为您提供 sql 式的功能。
This is not technically an answer but I feel its needed. Your approach is a bit backwards. What it sounds to me like you are really trying to do is use sharepoint as a UI to your data warehouse. If that is the case I would switch your approach to one of several options outside the scope of this question.
Data access in sharepoint is only supported by microsoft through the object model, web services or the user interface. Any interaction outside this can result in a vast range of unsupported modes, anything from corrupt data, data locks, varying result sets, security access, etc...
What it sounds like really need is the BDC services. This would allow you to use the data warehouse for central storage and also provide you with the sharepoint native functionality of the list interactions.
I'm not sure which version you are using but if you REALLY want to hit the data directly there are a large amount of articles in the SQL community about accessing the data directly. There is also a codeplex project for LINQtoSharePoint http://linqtosharepoint.codeplex.com/ You can use this for object support that you can use LINQ to give you sql-esque type functionality.
我非常简单的类从字符串 sql 转换为 caml ,例如:
sql = ....
CAML 输出是 .....
Class src:
(bob.)
My very simple class convert from string sql to caml , for example:
sql = ....
CAML output is .....
Class src:
(bob.)
如果您使用某种用于 SharePoint 的 ado.net 连接器,则可以查看 http:// /www.bendsoft.com/net-sharepoint-connector/
它使您能够与 SharePoint 列表进行对话,就像它们在普通 sql 表中一样
在示例中插入一些数据
或将列表数据选择到 DataTable
或使用助手填充 DataGrid 的方法
以下是一个网络广播,说明如何为 SharePoint 构建简单的查询浏览器,http:// www.youtube.com/watch?v=HzKVTZEsL4Y
希望有帮助!
It's possible if you use some sort of ado.net connector for SharePoint, have a look at http://www.bendsoft.com/net-sharepoint-connector/
It enables you to talk to SharePoint lists as if they where ordinary sql tables
In example to insert some data
Or to select list data to a DataTable
Or using a helper method to fill a DataGrid
Here is a webcast illustrating how to build a simple querybrowser for SharePoint, http://www.youtube.com/watch?v=HzKVTZEsL4Y
Hope it helps!
Microsoft 通过以下方式支持 SharePoint 列表数据访问:
1. SharePoint 对象模型 – SPSite 和 SPWeb
2. 列出 Web 服务。访问路径为 http://Your_Site/_vti_bin/lists.asmx
任意添加/更新/删除/选择SharePoint 列表上的操作是使用上述两种方法中的任何一种通过非托管 COM 组件完成的。该COM负责建立与Content DB的连接;在表和数据检索上应用数据锁。该 COM 组件有自己的逻辑/机制来在内容 DB 表上应用数据锁定,并且 Sharepoint 开发人员无法控制数据锁定机制。如果直接在内容数据库表上执行 T-SQL 语句(添加/更新/删除/选择),则此内置逻辑可能会中断,并可能导致未知结果或错误。 Microsoft 不支持在 Content DB 表上直接执行 T-SQL 语句。
Microsoft supports SharePoint List data access through:
1. SharePoint Object Model – SPSite and SPWeb
2. Lists web service. Path to access is http://Your_Site/_vti_bin/lists.asmx
Any Add/Update/Delete/Select operations on the SharePoint List is done using any of the above 2 methods goes through the unmanaged COM component. This COM is responsible for establishing the connection with Content DB; applying Data Locks on the Tables and data retrieval. This COM component has its own Logic/Mechanism to apply Data Lock on the Content DB tables and Sharepoint developer does not have control over the Data Lock mechanism. If T-SQL statements (Add/Update/Delete/Select) are executed directly on the Content DB tables, this In-Build logic may break and can be resulted into unknown results or errors. Microsoft does not support direct T-SQL statement execution on Content DB tables.
我已经使用以下 CAML 检查了 Francis 发布的代码:
它不起作用...在这种情况下,结果将是: F1<>'Yes' AND ( F2<>'Yes' OR F3 ='是')。
我在以下方法中做了一些修复:
这会将括号内的夫妇分组......仅此而已
i have checked the code posted by Francis with the following CAML:
And it doesn't work... the result, in this case, will be: F1<>'Yes' AND ( F2<>'Yes' OR F3='Yes' ).
I made some fixes inside the following method:
This will group the couple inside the parentheses ... and that's all
感谢您的帮助,我也需要它,您的代码很好,但我改进了一点:
请小心代码不会转义列名称(您必须根据您的数据库引擎来执行此操作)
这里是静态类中的代码:
thanks for your help, i need that too, your code was good but i improve it a little :
be careful the code doesn't escape the column name (you have to do it depending of your database engine)
here is the code in a static class: