Friday, June 3, 2016

Quick find on Selected View

Overview

Reference - 

“QuickFind doesn’t respect current view? I hear you!”

Thank you for your recent feedback regarding QuickFind functionality in CRM. The QuickFind search functionality (in its current form) does not return records from within the selected view. Instead, it spans its search across all active records within the selected entity. I have therefore written the following project as my answer to this problem.
Project Description Dynamics CRM solution : Do QuickFind search for selected view.
The purpose of this project is to distribute the QuickFind on Selected View solution and source code to the customer/partner community. I have developed this solution in response to the requests from customers and partners who wanted QuickFind to search under currently selected view. I have used Custom Entity and Plug-in to achieve some scenario but the requirement will vary from project to project, so I publishing this as an open source so that you can decide how to use it in your respective project.
Limitations
There are several known limitations.
– If you do QuickFind right after using Advanced Find, QuickFind may query by using Advanced Find query as that could be last used query. The Plug-in cannot distinguish between View query to Advanced Find query. 
– Same limitation applies If you do QuickFind right after you see Related Entity Grid for a record in separate window, QuickFind may query by using Related Entity Grid view 
– If you use Multi-Entity Search on Microsoft Dynamics CRM Table client, the search result may be affected by this solution, as it uses QuickFind feature to search the result.
Workaround
– If you think query results are not correct, refresh the view once more, which ensure the selected view definition is stored in last executed query. 

Solution Detail

This solution consists of 3 components.
Custom Entity : Query History
The custom entity stored “Last Executed Query” information. Please note it is not “Last Used View” information.
It has 2 custom fields.
– Entity Name : (1 line text, Length 100) – Stores which entity for the query
– FetchXML : (Multiline text, Length 10,000) – Stores last used FetchXml. (QueryExpression will be converted to FetchXML to store)
Plug-in
Plug-in monitor and investigate query passed to RetrieveMultiple, and decide what to do depending on the query. Please see source code for more detail.
If you want to register more steps, please use the Plugin Registration Tool and register a step as following.
– Register against RetrieveMultiple request (Please do not register against Execute)
– Specify the Entity that you want QuickFind query based on last executed query (Please do not leave primary Entity blank as that affect all entities)
– Specify Calling User to Run in User’s Context
– Register to Pre-Operation stage with Synchronous
Security Role
Simple security role which has User level Create/Read/Write privilege to the custom entity.

Important Notes

Please do not use this in ProductionPlease consider this as just a sample to demonstrate how to achieve the scenario. As scenario and situation varies depending on each environment, please investigate the code and modify accordingly to meet your requirements.
In addition, please implement error handling which suits your system.
Signing Key
Provided sample Visual Studio solution includes self signed certificate with no password. When you do so, please do not forget to update the entity and the field names in the plug-in code.
Managed Solution
I strongly recommend you to create your own solution which includes necessary custom entity with custom field. When you do so, do not forget update entity and field names in plug-in code.