Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query your data in the way you want to. A good example of this is the lack of left outer join support if you want a where clause to filter results based on the joined entity. Sometime, you just need to query your database using good old T-SQL. In CRM 4 you could do this fairly easily by simply opening a connection directly and doing what you need to.

Since Transactional Pipelines were introduced with CRM2011 , I've been dancing a jig every time I don't have write manual rollback compensation code – but – if you try a SQL query from a transactional pipeline that queries the same entity that you are updating/inserting, you'll get a blocking lock that will cause the operation to time out.

To get around this, you have a number of options:

1) Call the SQL from outside a transaction in the PreValidation or an Async Pipeline

2) Use the following method to hook into the CRM Transaction and execute your query from within that.

Note: I should say that this could be considered breaking the following rule in the SDK that defines what is supported or not:

"The use of application programming interfaces (APIs) other than the documented APIs in the Web services DeploymentService, DiscoveryService, Organization Data Service, SOAP endpoint for Web Resources and OrganizationService."

I'm assuming that you are familiar with the System.Data library, so I'm just posing how to get a SqlTransaction, and you can do the rest:

Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
object platformContext = context.GetType().InvokeMember("PlatformContext", System.Reflection.BindingFlags.GetProperty, null, context, null);
SqlTransaction tx = (SqlTransaction)platformContext.GetType().InvokeMember("SqlTransaction", System.Reflection.BindingFlags.GetProperty, null, platformContext, null);
DataSet result = SqlHelper.ExecuteDataset(tx, CommandType.Text, "SELECT ...");

  

You can also call a stored procedure in a different database that points back to the MSCRM database if you have complex queries. You'll need to use 'SET TRUSTWORTHY ON' to ensure that the security context is passed between the two databases.

My advice would be to only use this method only where using the SDK is just not possible or performs too slowly.

Hope this helps.

from: http://www.develop1.net/public/post/SQL-Queries-from-Transactional-Plugin-Pipeline.aspx

最新文章

  1. 让tomcat启动更快的设置
  2. HTTP和HTTPS
  3. .NET开源插件内核
  4. 使用spring的特殊bean完成配置
  5. Metro UI 菜单(Winform)
  6. node js 常用模块
  7. C#获取网页中某个元素的位置,并模拟点击
  8. 关于HTML的总结
  9. libtcmalloc 简单使用
  10. java--工具方法
  11. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
  12. tomcat session失效时间
  13. JDK1.8源码(十)——java.util.LinkedHashSet类
  14. SQL注入之Sqli-labs系列第四十六关(ORDER BY注入)
  15. 配置wildfly10为linux的服务,并开机启动
  16. 微信小程序- 提示不在以下合法域名列表中
  17. cubieboard安装小记
  18. kali 2018.1安装教程
  19. Swift3.0字符串相关操作
  20. JMeter学习笔记(五)-总结

热门文章

  1. 轻量级前端MVVM框架avalon - ViewModel
  2. es6学习笔记一数组(中)
  3. 在Windows环境中开始Docker的学习和体验
  4. 如何利用Python生成随机密码
  5. 【记录】JS 获取 URL 中文参数编码
  6. machine learning基础与实践系列
  7. OpenCV2:Mat属性type,depth,step
  8. 在SQL Server里我们为什么需要意向锁(Intent Locks)?
  9. byte为什么要与上0xff?
  10. iOS 视图与视图层次结构(内容根据iOS编程)