Hi,
I've been following and cross-referencing several tutorials with regards to implementing FetchXML inside javascript code. I have followed several documentation and the general structure is always the same. Create a fetchxml query in a string and pass it to an HttpRequest object. However I keep getting error when trying to execute the query stating Xrm is not defined. How do we define it? Does someone have a link to a more complete example? Seems like I might be missing something.
I would not recommend doing it direct from a plain HTML page to Dataverse. Not least of which you have to authenticate with Dataverse, and in so doing expose a set of login credentials on your page.
The piece of code you have probably works if run inside a Dataverse form from a Web Resource (even with the deprecated Xrm.Page uasage), but wont work in a stand alone html page due to not authenticating and the Xrm object being provided from Dataverse (Note the Xrm usage in that code just dynamically gets a path to the Dataverse instance and the second usage is just to display something in a pop, these can be hard coded and substituted for a JavaScript alert() respectively.
But your XMLHttpRequest would need to be extended to include authentication.
Not 100% sure, but you may also need to change from GET to POST (Microsoft try to force the use of POST these days)
I am trying to run the code through javascript on the html page. I do intend to focus on PowerApps Portal and am probably using the wrong piece of code to query a datasource in the dataverse.
I am attempting to use fetchXML to query different datasources (not just the datasource belonging to the form) within the javascript script tags because I need to filter the fetchXML queries using javascript variables.
Where are you trying to run the code from (answer will be different based on this)? this is a PowerApps Portal forum, if from Dynamicis 365 or somewhere else this is not the forum to ask this question (but someone probably knows the answer), also whilst it still works Xrm.Page is a deprecated namepace and you would use a formcontext object instead.
I suspect you are not trying to run it from a Powerapps portal, but if you are Xrm.Page is not available and it is easiest to use the Portals Web API and the "safeajax" code provided by Microsoft https://learn.microsoft.com/en-us/power-apps/maker/portals/write-update-delete-operations#wrapper-ajax-function (if all you need is to run the fetchxml on page load then you can use the Liquid fetchxml tag and iterate the results in Liquid.
WarrenBelz
9
Most Valuable Professional
Rondel
6
oliver.rodrigues
4
Most Valuable Professional