web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / FetchXML with javascri...
Power Pages
Answered

FetchXML with javascript error Xrm not defined

(0) ShareShare
ReportReport
Posted on by 167

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.

 

gospa_1-1671556051679.png

 

gospa_0-1671556329033.png

 

 

gospa_0-1671555906906.png

 

Categories:
I have the same question (0)
  • Verified answer
    Fubar Profile Picture
    8,346 Super User 2025 Season 2 on at

    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.

  • gng Profile Picture
    167 on at

    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.

  • Fubar Profile Picture
    8,346 Super User 2025 Season 2 on at

    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)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 70 Super User 2025 Season 2

#2
Jerry-IN Profile Picture

Jerry-IN 55

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard