Create a custom advanced find with sql query

时间:2015-07-28 23:02:31

标签: c# sql sql-server dynamics-crm dynamics-crm-2013

I want to be able to execute a sql query within crm so that I can interact with results. They are now an SSRS report but I dont want them read-only. I am stumped on how to do this. I found this Is it possible in Dynamics CRM to run an SQL query like an advanced find and include selection boxes? but I would like some more elaboration. Thanks!

Here is some C# because I thought maybe it has to be a plug in.

   public Class1()
   {
    using (SqlConnection connection = new SqlConnection("Data Source=AAHOASQL;Initial Catalog=DEVAAHOA_MSCRM"))
    using (SqlCommand cmd = new SqlCommand("dbo.BP_GetNearbyContacts", connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("ZipCode", zipCode);
        cmd.Parameters.AddWithValue("GivenMileRadius", givenMileRadius);
        connection.Open();
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            adapter.Fill(table);
        }
    }

2 个答案:

答案 0 :(得分:0)

The way I would implement this is by creating an HTML Web Resource which can be launched from the navigation menu.

This HTML Web Resource will query CRM using ODATA and the results will be presented in a table make sure your query returns the record's GUID, so you can create a hyperlink which will allow the users to open the record in CRM using URL Addressable Forms and Views.

Note that you will need to get an instance of the CRM context inside your HTML Web Resource for your ODATA query and URL Addressable forms. The CRM context will have useful information such as CRM Server, current user in context, CRM Organisation.

答案 1 :(得分:0)

理论上,你可以用一个插件和一些疯狂的黑客来做到这一点......解决方案会是这样的:

  1. 创建(或使用现有的)包含您要返回的属性的实体。 CRM高级查找仅包含已定义的属性。因此,如果您想要增加CRM实体的其他数据,则需要在实体上创建适当的属性来容纳数据。您只能在视图/高级查找中使用它们,但您仍然需要创建它们,它仍然会在后端的SQL表中创建字段。

  2. 创建一个名为SearchSql的附加列,它是一个布尔值,始终默认为yes,并且从不放在表单上。

  3. 创建一个在RetrieveMultiple PreEvent上触发的插件,该插件检查QueryExpression并确定SearchSql是否标记为是并将其存储在共享插件参数中,然后将其翻转为false(从CRM中的所有数据都将标记为false的字段,如果要返回10个实体,则该约束将返回0)。

  4. 创建在RetrieveMultiple PostEvent上触发的另一个插件。检查SearchSql共享插件参数。如果它在那里,那么 您可以在插件上下文中读取结果实体,并使用它来运行SQL查询。然后解析SQL查询的结果,并在相关实体上填充批准属性。