-
Notifications
You must be signed in to change notification settings - Fork 1
/
RetrieveMultipleWithQuickFindExample.cs
82 lines (72 loc) · 3.16 KB
/
RetrieveMultipleWithQuickFindExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
using System.Xml.Linq;
/// <summary>
/// This code was made by Maksym Vikarii
/// https://github.com/MaTeMaTuK
/// </summary>
namespace RetrieveMultipleDemo
{
/// <summary>
/// This is an example of Plugin on RetrieveMultiple message with quick find using. Plugin works with Unified Interface
/// </summary>
/// <remarks>
/// Register this plug-in on the RetrieveMultiple message, on pre validation and account entity.
/// </remarks>
public class RetrieveMultipleWithQuickFindExample : IPlugin
{
ITracingService tracingService;
IPluginExecutionContext executionContext;
public void Execute(IServiceProvider serviceProvider)
{
tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
executionContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (executionContext.InputParameters["Query"] is FetchExpression)
{
tracingService.Trace("Context query is a Fetch Expression");
SetFilterForFetchExpression();
}
}
void SetFilterForFetchExpression()
{
var fetchExpression = executionContext.InputParameters["Query"] as FetchExpression;
tracingService.Trace("Original query: {0}", fetchExpression.Query);
var fetchXmlDoc = XDocument.Parse(fetchExpression.Query);
var findValue = GetQuickFindValue(fetchXmlDoc);
if (string.IsNullOrEmpty(findValue))
return;
var newFilter = new XElement(
"filter",
new XAttribute("type", "and"),
new XElement(
"condition",
new XAttribute("attribute", "address1_fax"),
new XAttribute("operator", "like"),
new XAttribute("value", $"%{findValue}%")
));
var quickFindFieldsFilter = fetchXmlDoc.Descendants("filter")
.Where(f => f.Attribute("isquickfindfields") != null)
.FirstOrDefault();
//Attaching to quick find filter
quickFindFieldsFilter.Add(newFilter);
var newQuery = fetchXmlDoc.ToString();
tracingService.Trace("New Query with products: {0}", newQuery);
fetchExpression.Query = newQuery;
}
string GetQuickFindValue(XDocument xDocument)
{
var filters = xDocument.Descendants("filter");
var quickFindFilter = filters.Where(f => f.Attribute("isquickfindfields") != null).FirstOrDefault();
if (quickFindFilter == null)
return string.Empty;
var firstElement = quickFindFilter.FirstNode as XElement;
var quickFindValue = firstElement.Attribute("value").Value;
quickFindValue = quickFindValue.Replace("%", "");
return quickFindValue;
}
}
}