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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Using FetchXML in Web ...
Power Pages
Unanswered

Using FetchXML in Web API Queries via JavaScript for Dynamics 365 giving 401 error while sending request to the entity

(2) ShareShare
ReportReport
Posted on by 8

I have created one advance form in the power portal and in the first step of the form i'm trying to get the Contact information of the current user from Contacts entity.

For this I have written the below javascript code under from definition section to get the data.

 

 

$(document).ready(function(){var fetchXmlQuery = ` 
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
 <entity name="contact">
 <attribute name="fullname" />
 <attribute name="cr0d4_companyname" />
 <attribute name="contactid" />
 <attribute name="description" />
 <attribute name="websiteurl" />
 <order attribute="fullname" descending="false" />
 <filter type="and">
 <condition attribute="statecode" operator="eq" value="0" />
 <condition attribute="fullname" operator="eq" value="{{ user.fullname }}" />
 </filter>
 </entity>
</fetch>`;

var req = new XMLHttpRequest();

req.open("GET", "https://Sample.crm5.dynamics.com/api/data/v9.0/cr0d4_incidentreports?fetchXml=" + 
 encodeURIComponent(fetchXmlQuery), true);

req.setRequestHeader("Accept", "application/json");

req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

req.onreadystatechange = function ()
{
 if (this.readyState === 4)
 {
 this.onreadystatechange = null;
 if (this.status === 200)
 {
 var returned = JSON.parse(this.responseText);
 alert(returned);
 }
 else
 {
 alert(this.status);
 }
 }
};
req.send();
});

 

 

Note I have created the permission for entity and added the web role and i can see the data while accessing request URL on the browser. 

 

Categories:
I have the same question (0)
  • ragavanrajan Profile Picture
    7,044 Most Valuable Professional on at

    Hi @Hiral2010 

     

    Probably related to API permissions for the portal sitting in Azure App registrations. Please have a look at the following thread. 

     

    https://xrmdynamicscrm.wordpress.com/2020/06/08/dynamics-crm-365-web-api-common-errors-and-resolution/ 

     

     

    Hope it helps. 
    ------------

    If you like this post, give it a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users to find it.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Pages

#1
Lucas001 Profile Picture

Lucas001 21 Super User 2026 Season 1

#2
Fubar Profile Picture

Fubar 19 Super User 2026 Season 1

#3
CN-06091549-0 Profile Picture

CN-06091549-0 18

Last 30 days Overall leaderboard