Skip to main content

Notifications

Community site session details
Power Pages - General Discussions
Answered

FetchXML with javascript error Xrm not defined

Like (0) ShareShare
ReportReport
Posted on 20 Dec 2022 17:07:41 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:
  • Fubar Profile Picture
    7,867 Super User 2025 Season 1 on 21 Dec 2022 at 03:41:49
    Re: FetchXML with javascript error Xrm not defined

    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)

  • gng Profile Picture
    167 on 20 Dec 2022 at 22:32:43
    Re: FetchXML with javascript error Xrm not defined

    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.

  • Verified answer
    Fubar Profile Picture
    7,867 Super User 2025 Season 1 on 20 Dec 2022 at 22:23:36
    Re: FetchXML with javascript error Xrm not defined

    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.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Pages - General Discussions

#1
WarrenBelz Profile Picture

WarrenBelz 9 Most Valuable Professional

#2
Rondel Profile Picture

Rondel 6

#3
oliver.rodrigues Profile Picture

oliver.rodrigues 4 Most Valuable Professional

Overall leaderboard
Loading started
Loading started