• Home
  • Blog
  • About Me
  • Home
  • Blog
  • About Me
Dynamics 365

Quick Find – PlugIn to modify the search

By Joe Gill  Published On 20th April 2015
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.

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

    }

}


Leave A Reply Cancel reply

You must be logged in to post a comment.

The Implications of Cascading Ownership
Previous Article
Business Process Flow Conditional Branching
Next Article