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 / 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

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
Jerry-IN Profile Picture

Jerry-IN 71

#2
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard