If you have been following my blog, you might remember this (https://dreamingincrm.com/2014/05/07/executing-quickfind-using-crm-sdk/) post about executing a quick find query from the console. It was using an undocumented message, and hence it is unsupported.
I had a crack at this problem one more time, this time using Actions.In order to return the quick find results, the custom action need to have a EntityCollection output parameter. I posted this (https://community.dynamics.com/crm/f/117/t/128534.aspx) question in CRM forums sometime back and didn’t get any response.
This is not production ready code and just demonstrates how this can be done. If you would rather read the code, instead of this post please find the download link in the very bottom.
Requirements:
For the search – should be able to specify:
1.) Entity name
2.) Search term
3.) Page number
4.) Number of records to be returned
Step 1: Create a custom action
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
using System.Xml.Linq;
using Contract = System.Diagnostics.Contracts.Contract;
namespace QuickFindAction.Plugins
{
public class QuickFindPlugin : IPlugin
{
internal IOrganizationService OrganizationService
{
get;
private set;
}
internal IPluginExecutionContext PluginExecutionContext
{
get;
private set;
}
internal ITracingService TracingService
{
get;
private set;
}
public void Execute(IServiceProvider serviceProvider)
{
Contract.Assert(serviceProvider != null, "serviceProvider is null");
PluginExecutionContext =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Contract.Assert(TracingService != null, "TracingService is null");
try
{
var factory =
(IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
OrganizationService = factory.CreateOrganizationService(this.PluginExecutionContext.UserId);
Contract.Assert(PluginExecutionContext.InputParameters.Contains("SearchTextInput"), "No SearchTextInput property");
Contract.Assert(
!string.IsNullOrEmpty(PluginExecutionContext.InputParameters["SearchTextInput"].ToString()), "SearchTextInput is null or empty");
Contract.Assert(PluginExecutionContext.InputParameters.Contains("EntityNameInput"), "No EntityNameInput property");
string searchText = PluginExecutionContext.InputParameters["SearchTextInput"].ToString(),
searchEntity = PluginExecutionContext.InputParameters["EntityNameInput"].ToString();
var savedViewQuery = string.Format(
@"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""false"">
<entity name=""savedquery"">
<attribute name=""fetchxml"" />
<filter type=""and"">
<condition attribute=""statecode"" operator=""eq"" value=""0"" />
<condition attribute=""isquickfindquery"" operator=""eq"" value=""1"" />
<condition attribute=""isdefault"" operator=""eq"" value=""1"" />
<condition attribute=""name"" operator=""like"" value=""%{0}%"" />
</filter>
</entity>
</fetch>", searchEntity);
var quickFindFetchXml =
OrganizationService.RetrieveMultiple(new FetchExpression(savedViewQuery)).Entities[0].GetAttributeValue<string>("fetchxml");
TracingService.Trace("FetchXml read from SavedView");
var entityFetchXml = XElement.Parse(string.Format(quickFindFetchXml, string.Format("%{0}%", searchText)));
if (PluginExecutionContext.InputParameters["Page"] != null)
{
entityFetchXml.SetAttributeValue("page", PluginExecutionContext.InputParameters["Page"]);
}
if (PluginExecutionContext.InputParameters["Count"] != null)
{
entityFetchXml.SetAttributeValue("count", PluginExecutionContext.InputParameters["Count"]);
}
entityFetchXml.Elements().Elements("filter").Elements().ToList().ForEach(x => {
if (
x.Attribute(
"attribute")
.Value
.EndsWith("id"))
{
x.SetAttributeValue("attribute",x.Attribute("attribute").Value+"name");
} });
PluginExecutionContext.OutputParameters["FetchXml"] = entityFetchXml.ToString();
var results = OrganizationService.RetrieveMultiple(new FetchExpression(entityFetchXml.ToString()));
PluginExecutionContext.OutputParameters["SearchResultsOutput"] = new EntityCollection(results.Entities.ToList());
}
catch (Exception e)
{
TracingService.Trace(e.StackTrace);
PluginExecutionContext.OutputParameters["Exception"] = e.StackTrace;
throw;
}
}
}
}



