xmldynamics-crmcrmfetchxmlxrm

FetchXml-Request does not work, if it contains 'page' (invalid XML)


I'm building a web-ressource for Dynamics CRM (8.2). When I try to fetch data with fetchXml like this, everything is fine:

<fetch mapping="logical" count="3" >
    <entity name="product" >
        <attribute name="name" />
        <attribute name="productnumber" />
    </entity>
</fetch>

but if i add the 'page' parameter, like in the next code, i get Invalid XML.

<fetch mapping="logical" count="3" page="1" >
    <entity name="product" >
        <attribute name="name" />
        <attribute name="productnumber" />
    </entity>
</fetch>

I tested the fetch with the FetchXml Tester from XrmToolBox. There it works fine. What did I do wrong?

I tried to do the request with the npm package dynamics-web-api (https://www.npmjs.com/package/dynamics-web-api) and with XMLHttpRequest. Both worked like discribed above.

The Error-Message is this: (sorry for the formatting)

{
  "error":{
    "code":"",
    "message":"Invalid XML.",
    "innererror":{
      "message":"Invalid XML.",
      "type":"System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]",
      "stacktrace":"   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.RetrieveMultiple(QueryBase query, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)
         at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.RetrieveMultiple(QueryBase query)
         at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.ExecuteQueryForEntitySet(CrmODataExecutionContext context, String entitySetName, CustomQueryOptions queryOptions, String fetchXml)
         at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.RetrieveEdmEntityCollection(CrmODataExecutionContext context, String entityCollectionName, String castedEntityName, ODataQueryOptions queryOptions)
         at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)
         at lambda_method(Closure , Object , Object[] )
         at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
         at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }
  }
  }

**Edit**
My error were somewhere in the formatting of the fetch-string. I used:

const query = encodeURI(connectionString + "?fetchXml=" + encodeURI(xmlString));

the correct code is:

const query = encodeURI(connectionString) + "?fetchXml=" + encodeURI(xmlString);


Ty AnkUser for your example. It helped me much.

Solution

  • I just tried with Page=1 with my below request and Fetchxml and it worked fine for me Note I used Account for my example, but it should not be an issue.

    <fetch mapping="logical" count="3" page="1" >
            <entity name="account" >
                <attribute name="name" />
                <attribute name="accountnumber" />
            </entity>
    

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?fetchXml=%3Cfetch%20mapping%3D%22logical%22%20count%3D%223%22%20page%3D%221%22%20%3E%3Centity%20name%3D%22account%22%20%3E%3Cattribute%20name%3D%22name%22%20%2F%3E%3Cattribute%20name%3D%22accountnumber%22%20%2F%3E%3C%2Fentity%3E%3C%2Ffetch%3E", false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();