SharePoint:验证 SPQuery
我正在为我的公司编写一个内部 API,它允许用户传递 SharePoint CAML 查询。 在我的函数中,我接受用户的查询,并向其中添加一些附加元素,然后使用最终查询从 SharePoint 检索所需的数据。
示例:
用户传入:
<Query>
<Where>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
</Where>
</Query>
在内部,我将查询修改为:
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
<Eq>
<FieldRef Name='AssignedTo' />
<Value Type='Integer'><UserID /></Value>
</Eq>
</And>
</Where>
</Query>
您认为验证用户发送的查询的最佳方式是什么?
I am writing an internal API for my company which allows users to pass in SharePoint CAML query.
In side my function I take to user's query, and add some additional elements to it and then use the final query to retrieve required data from SharePoint.
Example:
User passes in:
<Query>
<Where>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
</Where>
</Query>
Internally, I modify the query to be:
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
<Eq>
<FieldRef Name='AssignedTo' />
<Value Type='Integer'><UserID /></Value>
</Eq>
</And>
</Where>
</Query>
What do you think is the best way to validate queries sent by users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要允许的查询相当有限,那么构建一个架构来表示有效查询可能是一个好方法。然后您可以根据该架构查看它们的 xml 是否有效。另外,我知道您可以从代码中使用 CAML Builder dll。我无法立即找到这样的示例,但可能有一种方法可以在 try/catch 块中使用它的 CAML 构建方法来阻止构建无效查询。
另外,我发现您可能需要注意以下事实:CAML 查询的 FieldRef 需要使用字段的内部名称来构建,该内部名称可能与显示名称不同。
If the queries that you're going to allow are fairly restricted, it might be a good approach to build a schema to represent what a valid query would be. Then you could just see if their xml is valid according to that schema. Also, I know that you can use the CAML Builder dll from code. I can't find an example of this right away, but there may be a way to use it's CAML building methods in a try/catch block to stop invalid queries from ever getting built.
Also, it occurs to me that you may need to watch out for the fact that the CAML query's FieldRef will need to be built using the internal name of the field which may differ from the display name.