I prefer FetchXml compared to QueryExpression or LINQ when writing custom code. Back during the CRM3/4 days there was Stunnware. CRM2011 introduced capability to export the FetchXml directly from the Advanced Find. In CRM 2013/2015 you have four tools, to write and export the FetchXml.
- FetchXml Tester (comes with XRMToolBox)
- FetchXml Builder
- Fetch Tester 3000
- CRM DevTools (Chrome only)
What if there is an better, albeit unsupported way? You can embed the advanced find on the entity form that needs to store the fetchxml, and get the fetchxml from the advanced find, when the user saves the record.
Step 1:
You need two controls: a text area to store the fetchxml and an IFrame that will display the advanced find. The target of the IFrame is "about: blank". We will set the correct URL using Javascript. I have added the IFrame to a seperate tab, and the default state of this tab is collapsed.
Step 2:
Add the script below as a Javascript webresource and hookup the onSave and onLoad functions to the form save and form load respectively. The id of my IFrame is IFrame_Advanced and the fetchxml is stored in textarea ryr_fetchxml.
var RYR = window.RYR || {}; RYR.onLoad = function(){ Xrm.Page.getControl('IFRAME_AdvancedFind').setSrc(Xrm.Page.context.getClientUrl()+'/advancedfind/advfind.aspx?pagemode=iframe&navbar=off&cmdbar=false'); }; RYR.onSave = function(){ var advancedFindFrame = document.getElementById('IFRAME_AdvancedFind').contentWindow; advancedFindFrame.ExecuteQuery(); advancedFindFrame.ShowQuery(); Xrm.Page.getAttribute('ryr_fetchxml').setValue(advancedFindFrame.document.getElementById('FetchXml').value); }; window.RYR = RYR;
Here is how it looks.
The trick is to call ShowQuery straight after ExecuteQuery, so that div with id FetchXml contains the correct value. If we don't call the ExecuteQuery, the value of FetchXml won't be updated when the advanced find is modified by the user.
Once again, this technique is unsupported, so use it at your own risk. I have tested this in Firefox 34 and CRM 2013 6.1.0.581.
UPDATE (24/12/14): There was an issue with the initial script, as it was only working if the organisation was a default one. The initial script split the screen horizontally, if the IFrame url was correctly set using getClientUrl. The updated script fixes this issue. Based on my testing, this is working in Firefox 34, Chrome 39.0.2171.95 and IE 11(in IE9 mode only). One more thing to note -> If the tab with the Advanced Find IFrame doesn't started out collapsed, the Ribbon Interface of the Advanced Find shows up on the form.
No comments:
Post a Comment