Quick Find – PlugIn to modify the search

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.

Quick Find - PlugIn to modify the search Joe Gill Dynamics 365 Consultant & Microsoft Dynamics MVP

As a result when I do a quick find it only returns account records I own.

Quick Find - PlugIn to modify the search Joe Gill Dynamics 365 Consultant & Microsoft Dynamics MVP

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.

Quick Find - PlugIn to modify the search Joe Gill Dynamics 365 Consultant & Microsoft Dynamics MVP

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.

public void Execute(IServiceProvider serviceProvider)
{
        Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
        serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

        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;
        }

    }

}

Quick Find - PlugIn to modify the search Joe Gill Dynamics 365 Consultant & Microsoft Dynamics MVP

Leave a Reply