cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Dhananjay_Patil
Helper IV
Helper IV

How to execute FetchXML query on button click

Hello 

Am using FetchXML to fetch entity records, but How can I pass dynamic parameters in FetchXML and execute FetchXML query on particular even like button click in PowerApps Portal?

Example:
I want to execute below FetchXML query on button click with some dynamic parameters in value field 

 

 

{% fetchxml DataList %}    
<fetch version="1.0" mapping="logical" top="50">
        <entity name="contact">
            <filter>
            <condition attribute ="fullname" operator ="eq" value="Dhananjay"/>
            </filter>
        </entity>
    </fetch>
{% endfetchxml %}

 

3 ACCEPTED SOLUTIONS

Accepted Solutions
justinburch
Microsoft
Microsoft

Hi @Dhananjay_Patil,

You'll want to put that fetch in a separate web page and call it with AJAX. This is a common approach in Portal development.

  1. Create a Web template with your code and a JSON return value, and set the MIME type at the bottom of the record as "application/json"

 

{% assign fullname = params.fullname %}
{% fetchxml DataList %}    
<fetch version="1.0" mapping="logical" top="50">
        <entity name="contact">
<attribute name="contactid" />
            <filter>
            <condition attribute ="fullname" operator ="eq" value="{{ fullname | xml_escape }}"/>
            </filter>
        </entity>
    </fetch>
{% endfetchxml %}
{
"results": [
{% for result in DataList.results.entities %}
"{{ result.contactid | escape }}"{% unless forloop.last %},{% endunless %}
{% endfor %}
]}​

 

 

  • This will return back a JSON object like so, but you could return an array of objects instead of strings if you need to get more data

 

{
  results: ["00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002"]
}​

 

 

  • Create a Page Template for this Web Template, do not enable the "Use Headers and Footers" option
  • Create a Web Page using this Web Template, make the partial url something easy like "get-contacts"
  • In the JS for your button, call and use this data - example to output the number of results found in the dev console here

 

 

$("#your-button-id").click(function () {
  $.ajax(
    method: 'GET',
    dataType: 'json',
    url: "/get-contacts?fullname=" + encodeURIComponent(myFullNameVariableHere)
  }).done(function(data) {
    console.log("I found " + data.results.length + " result(s)");
  });
});​

 

 

View solution in original post

Hi @Dhananjay_Patil,

Despite other places in your website not requiring entity permissions, any requests done through Liquid or the new API will require proper Entity Permissions are setup for the Web Role of the user. More likely than not, your user does not have appropriate permissions to access the data.

View solution in original post

Hi @Dhananjay_Patil,

While you can use POST, you won't have access to the request body, so it's basically the same as the GET. For anything like this, you'll want to utilize a true API with implicit grant flow, as detailed here: https://docs.microsoft.com/en-us/powerapps/maker/portals/oauth-implicit-grant-flow

View solution in original post

13 REPLIES 13
hardikv
Solution Sage
Solution Sage

Hi @Dhananjay_Patil 

You can pass parameter or value using liquid code only. You can not pass/add Javascript variable in fetchxml or liquid code. If you have query string parameter then you can pass it to liquid variable.

function GetData()
{
  {% assign fullnameData = {{user.fullname}} %}  
  
  //if you have query string parameter then you can also assign to this variable
  //{% assign fullnameData = request.params['fullname'] %} 

  {% fetchxml DataList %}    
  <fetch version="1.0" mapping="logical" count="50">
      <entity name="contact">
          <filter>
            <condition attribute="fullname" operator="eq" value='{{fullnameData}}' />
          </filter>
      </entity>
  </fetch>
  {% endfetchxml %}
}

--------------------------
If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

justinburch
Microsoft
Microsoft

Hi @Dhananjay_Patil,

You'll want to put that fetch in a separate web page and call it with AJAX. This is a common approach in Portal development.

  1. Create a Web template with your code and a JSON return value, and set the MIME type at the bottom of the record as "application/json"

 

{% assign fullname = params.fullname %}
{% fetchxml DataList %}    
<fetch version="1.0" mapping="logical" top="50">
        <entity name="contact">
<attribute name="contactid" />
            <filter>
            <condition attribute ="fullname" operator ="eq" value="{{ fullname | xml_escape }}"/>
            </filter>
        </entity>
    </fetch>
{% endfetchxml %}
{
"results": [
{% for result in DataList.results.entities %}
"{{ result.contactid | escape }}"{% unless forloop.last %},{% endunless %}
{% endfor %}
]}​

 

 

  • This will return back a JSON object like so, but you could return an array of objects instead of strings if you need to get more data

 

{
  results: ["00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002"]
}​

 

 

  • Create a Page Template for this Web Template, do not enable the "Use Headers and Footers" option
  • Create a Web Page using this Web Template, make the partial url something easy like "get-contacts"
  • In the JS for your button, call and use this data - example to output the number of results found in the dev console here

 

 

$("#your-button-id").click(function () {
  $.ajax(
    method: 'GET',
    dataType: 'json',
    url: "/get-contacts?fullname=" + encodeURIComponent(myFullNameVariableHere)
  }).done(function(data) {
    console.log("I found " + data.results.length + " result(s)");
  });
});​

 

 

View solution in original post

@hardikv 

How can I pass javascript variable to FetchXML?

Hello @justinburch 

In my application web pages I need to use header and footer, Can I define FetchXml in another page template and can use in my application actual web pages?

Hi @Dhananjay_Patil,

Only the Web Page used as an API endpoint should have no footer/header - it's not a Web Page any users should ever see directly. To be clear, you should have the following:

  1. One Web Page that shows your information and button
    1. Page Template with header and footer = enabled
  2. One Web Page to be your API "endpoint"
    1. Page template with header and footer = disabled
      1. Page template should have similar code to what I provided before

Hello @justinburch 

I did the same thing as u mentioned but am geeting { "results": [ ]}  always 

Hi @Dhananjay_Patil,

Despite other places in your website not requiring entity permissions, any requests done through Liquid or the new API will require proper Entity Permissions are setup for the Web Role of the user. More likely than not, your user does not have appropriate permissions to access the data.

View solution in original post

Hello @justinburch 

Enabling Entity Permission works for me.

$("#your-button-id").click(function () {
  $.ajax(
    method: 'GET',
    dataType: 'json',
    url: "/get-contacts?fullname=" + encodeURIComponent(myFullNameVariableHere)
  }).done(function(data) {
    console.log("I found " + data.results.length + " result(s)");
  });
});

 For GET method its working fine , Can I use Post method with body parameters?

Hi @Dhananjay_Patil,

While you can use POST, you won't have access to the request body, so it's basically the same as the GET. For anything like this, you'll want to utilize a true API with implicit grant flow, as detailed here: https://docs.microsoft.com/en-us/powerapps/maker/portals/oauth-implicit-grant-flow

View solution in original post

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Users online (3,581)