The Quick Find functionality in Dynamics CRM allows users to quickly search for records. The Quick Find view for each entity is used to customize how the records are filtered for searching. Typically the filtering criteria restricts the searching so that only records owned by the current user are returned.
A common request from users is the ability to extend the records to be searched. In this post I am going to show how a custom plugin can be used to modify the search’s filtering criteria on the fly. This is a simple example however it can easily be extended to cover other scenarios. The Account Quick Find view filter is configured to search active records where the owner is the current user.
As a result when I do a quick find it only returns account records I own.
Utilizing our plugin when the user enters a percentage sign as part of the search string then the filtering condition that restricts the owner to the current user is removed.
The QuickFindExtender plugin is registered on the RetrieveMultiple event of the account entity at the pre-validation stage. The plugin intercepts the query and searches the FetchXML to see if the user entered a percentage sign part of their search string. If it contains a percentage sign then the filtering condition that restricts the owner to the current user is removed.
context = (Microsoft.Xrm.Sdk.IPluginExecutionContext
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
FetchExpression query = (FetchExpression)context.InputParameters[“Query”];
string fetchXml = (context.InputParameters[“Query”] as FetchExpression).Query;
fetchXml = fetchXml.Replace(@”<condition attribute=””ownerid”” operator=””eq-userid”” />”, “”);
fetchXml = fetchXml.Replace(@”[%]%”, “%”);
query.Query = fetchXml;
context.InputParameters[“Query”] = query;