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.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (!context.InputParameters.Contains(“Query”))
return;
if (context.InputParameters[“Query”].GetType().Equals(typeof(FetchExpression)))
{
FetchExpression query = (FetchExpression)context.InputParameters[“Query”];
string fetchXml = (context.InputParameters[“Query”] as FetchExpression).Query;
if (fetchXml.Contains(@”[%]%”))
fetchXml = fetchXml.Replace(@”<condition attribute=””ownerid”” operator=””eq-userid”” />”, “”);
fetchXml = fetchXml.Replace(@”[%]%”, “%”);
query.Query = fetchXml;
context.InputParameters[“Query”] = query;
}
}
}