I'm creating search shortcuts from the quick search box for certain entities. This is to avoid multiple returns especially when a name could contain a city name. (City searches are relevant, so it has to stay)
I'm accomplishing this via a plugin. So a user enters
/name Todd Richardson
In the search box on the contact entity view.
Update
This intercepts (pre-operation stage:20 prevalidation stage:10) the Retrievemultiple request for a contact.
End Update
Update As requested here is the beginning of the implementation as generated and then modified from the MSCRM 2011 sdk tools Please remember that this code is in a prototypical state, and may not be suitable for production code:
protected void ExecutePreAccountRetrieveMultiple(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
if (localContext.PluginExecutionContext.InputParameters.Contains("Query"))
{
if (localContext.PluginExecutionContext.InputParameters["Query"] is QueryExpression)
{
//query expression from input is assigned to a local variable for modification.
QueryExpression qe = (QueryExpression)localContext.PluginExecutionContext.InputParameters["Query"];
if (qe.Criteria != null)
{
if (qe.Criteria.Filters.Count > 1)
{
string entitySubject = qe.EntityName;
string searchSubject = qe.Criteria.Filters[1].Conditions[0].Values[0].ToString();
string namePattern = @"^([/\\-])+N(AME)?:?[\s]*(.+$)";
//.... Eliminated for brevity, only including branch thats relevant to this question.
if (Regex.IsMatch(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase))
{
var Match = Regex.Match(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase);
if (Match.Groups.Count > 1)
{
int lastIndex = Match.Groups.Count - 1;
string name = Match.Groups.Cast<Group>().Last().Value;
Func<string, List<ConditionExpression>> genXpress = (n) =>
{
List<ConditionExpression> ce = new List<ConditionExpression>();
foreach (var val in name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => string.Format("%{0}%", x)))
{
ce.Add(new ConditionExpression
{
Operator = ConditionOperator.Like,
AttributeName = n,
Values = { val }
});
}
return ce;
};
if (entitySubject == "contact")
{
string[] names = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (names.Length > 1)
{
string fn = names[names.Length - 2];
string ln = names[names.Length - 1];
string fetchRequest =
@"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0"">
<entity name=""contact""> <attribute name=""fullname""/>
<attribute name=""telephone1""/> <attribute name=""contactid""/>
<order descending=""false"" attribute=""fullname""/>
<filter type=""and"">
<filter type=""or"">
<filter type=""and"">
<condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/>
<condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/>
</filter>
<filter type=""and"">
<condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/>
<condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/>
</filter>
</filter>
</filter>
</entity>
</fetch>" //
.Replace("%lastname%", ln).Replace("%firstname%", fn);
var conversionRequest = new FetchXmlToQueryExpressionRequest
{
FetchXml = fetchRequest
};
var response = (FetchXmlToQueryExpressionResponse)localContext.OrganizationService.Execute(conversionRequest);
localContext.PluginExecutionContext.OutputParameters["Query"] = response.Query;
return;
}
//variable modified and now passed out for execution.
localContext.PluginExecutionContext.OutputParameters["Query"] = qe;
return;
}
}
} //Remainder of code eliminated for different logic branches.
End update
A query expression is generated and put into the output parameter named query.
Originally I was building the QueryExpression.I was finding that this did not work. No matter how I built my query expression, I was getting
condition1 || condition 2 || condition3 || condition4
So I took another angle. I went to the Advanced find and created a query that returned exactly what I wanted in the Results. I downloaded the fetch-xml and now this is what I have (as seen in the code previous):
string fetchRequest =
@"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0"">
<entity name=""contact""> <attribute name=""fullname""/>
<attribute name=""telephone1""/> <attribute name=""contactid""/>
<order descending=""false"" attribute=""fullname""/>
<filter type=""and"">
<filter type=""or"">
<filter type=""and"">
<condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/>
<condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/>
</filter>
<filter type=""and"">
<condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/>
<condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/>
</filter>
</filter>
</filter>
</entity>
</fetch>" //
Whether I was generating the Queryexpression in code, or fetching it from the organization service, it seems to get me the same result. Instead of
(condition1 && condition2) || (condition3 && condition4)
fulfilling the criteria, it basically ends up
condition1 || condition2 || condition3 || condition4
I've tried other variations on the fetch xml, including:
string fetchRequest = @"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0"">
<entity name=""contact""> <attribute name=""fullname""/>
<attribute name=""telephone1""/> <attribute name=""contactid""/>
<order descending=""false"" attribute=""fullname""/>
<filter type=""and"">
<condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/>
<condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/>
</filter>
</entity>
</fetch>"
Again this ends up being
condition1 || condition2
not
condition1 && condition2
Anyone have any clue as to what is going on. Is there a different fetchxml I should be using? Is this a bug? The answer has been elluding me for a better part of the day.
Hopefully it's just something easy that I'm overlooking.