omartin2010
omartin2010

Reputation: 461

fetchXML simple account retrieve

I'm trying to do a fetchXML query against my account list using the simple query provided here

Normally, I would expect this to respond with a json responses alongside the account I'm running a query for, but I'm instead getting this error message :

The request URI is not valid. Since the segment 'accounts' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource.

My actual URL request is : https://<org>.crm.dynamics.com/api/data/v9.0/accounts(<actual_guid>)

Am I overlooking something? I originally had a more complex fetchXML request but made it as simple as I can think of to eliminate possible issue.

Thanks in advance for any help!

Upvotes: 0

Views: 494

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

If you want to execute a FetchXML query against the Web API endpoint, the correct syntax is this one (jQuery example but you can see the endpoint url):

var originalFetchXML = '<fetch top="5" ><entity name="account" ><attribute name="accountid" /><attribute name="name" /></entity></fetch>';
var escapedFetchXML = encodeURIComponent(originalFetchXML);

$.ajax({
type: "GET",
url: Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/accounts?fetchXml=" + escapedFetchXML,
async: true,
headers: {
    "OData-MaxVersion": "4.0",
    "OData-Version": "4.0",
    "Content-Type": "application/json; charset=utf-8",
    "Accept": "application/json",
    "Prefer": "odata.include-annotations=*"
},
success: function (data, textStatus, xhr) {
    var results = data;
    console.log(results);
},
error: function (xhr, textStatus, errorThrown) {
    console.log(xhr);
}
});

you didn't write which exact language you are using to execute the query, but if is JavaScript (Xrm.WebApi, Fetch API, jQuery or XHR) you can use Dataverse REST Builder to generate it, executing Fetch XML is under the Predefined Query request type and Query Type as FetchXML

Upvotes: 1

Related Questions