i'm trying to create a rules table that would store predefined fetchXML queries to be used in in a C# plugin, i know that i can use the follwing code in a plugin
var value = "something";
var fetchXml = "<fetch>";
fetchXml += $"<entity name='account'>";
fetchXml += " <all-attributes />";
fetchXml += " <filter>";
fetchXml += $" <condition attribute='xxx_field' operator='eq' value='{value}'/>";
fetchXml += " </filter>";
fetchXml += "</entity>";
fetchXml += "</fetch>";
var entitySyncRules = service.RetrieveMultiple(new FetchExpression(fetchXml));
and it works no problem the code replaces the {vale} with something . now Ii'm trying to take this to an other level. i have a rule table entity that would store the fetchXML this would enable me to build and edit the fetchXML dinamicly
[rule entity]
storeFetchXML = <fetch><entity name='account'><all-attributes /><filter><condition attribute='xxx_field' operator='eq' value='{value}'/></filter></entity></fetch>
in the plugig
value = "something"
fetchXML = ruleentity.GetAttributeValue<string>("storeFetchXML")
var entitySyncRules = service.RetrieveMultiple(new FetchExpression(fetchXml));
this code do not work, it doest replace the {value} with the content on the variable. it leave the string as is .
is there a way to make sure that the code does the code replace . if tried the following
fetchXML = $ruleentity.GetAttributeValue<string>("storeFetchXML")
fetchXML = $"{ruleentity.GetAttributeValue<string>("storeFetchXML")}"
if i'm going down the wrong path... this is possible. but is there a way to use a stored parametrized fetchXML in a queue with the paramater being replace at run time. (yes i know the field must exist in the code)
Use string interpolation or string.Format and substitute in your values in an array.
e.g.
string.Format(query, paramvaluesArray);
Where query = normal fetch with substitution such as
<condition attribute='name' operator='eq' value='{0}' />
<condition attribute='websiteurl' operator='eq' value='{1}' />
The values in the Array are then substituted in sequence into to {0} etc
In our case we read from another entity the query, and a comma separated list of values. We convert the comma separated list to an Array and this allows us to vary the query conditions and the number of substituted values without code changes.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156