Skip to content

Commit 1a17b76

Browse files
add: fetch, fetchAll, executeFetchXmlAll, retrieveAll
1 parent 3fb15f7 commit 1a17b76

File tree

6 files changed

+438
-130
lines changed

6 files changed

+438
-130
lines changed

README.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Any suggestions are welcome!
3434
* [Disassociate](#disassociate)
3535
* [Disassociate for a single-valued navigation property](#disassociate-for-a-single-valued-navigation-property)
3636
* [Fetch XML Request](#fetch-xml-request)
37+
* [Fetch All records](#fetch-all-records)
3738
* [Execute Web API functions](#execute-web-api-functions)
3839
* [Execute Web API actions](#execute-web-api-actions)
3940
* [JavaScript Promises](#javascript-promises)
@@ -164,9 +165,9 @@ Basic calls can be made by using functions with most commonly used input paramet
164165
not provide all possible ways of interaction with CRM Web API (for example, [conditional retrievals](https://msdn.microsoft.com/en-us/library/mt607711.aspx#bkmk_DetectIfChanged)
165166
are not supported in basic functions).
166167

167-
Basic functions are: `create`, `update`, `upsert`, `deleteRecord`, `retrieve`, `retrieveMultiple`, `count`, `countAll`, `executeFetchXml`,
168-
`associate`, `disassociate`, `associateSingleValued`, `disassociateSingleValued`, `executeBoundFunction`, `executeUnboundFunction`,
169-
`executeBoundAction`, `executeUnboundAction`.
168+
Basic functions are: `create`, `update`, `upsert`, `deleteRecord`, `retrieve`, `retrieveMultiple`, `retrieveAll`, `count`, `countAll`,
169+
`executeFetchXml`, `executeFetchXmlAll`, `associate`, `disassociate`, `associateSingleValued`, `disassociateSingleValued`, `executeBoundFunction`,
170+
`executeUnboundFunction`, `executeBoundAction`, `executeUnboundAction`.
170171

171172
Advanced functions have a suffix `Request` added to the end of the applicable operation.
172173
Most of the functions have a single input parameter which is a `request` object.
@@ -568,15 +569,29 @@ dynamicsWebApi.retrieveMultipleRequest(request).then(function (response) {
568569

569570
#### Retrieve All records
570571

571-
Current function goes through all pages automatically.
572+
The following function retrieves records and goes through all pages automatically.
573+
574+
```js
575+
//perform a multiple records retrieve operation
576+
dynamicsWebApi.retrieveAll("leads", ["fullname", "subject"], "statecode eq 0").then(function (response) {
577+
578+
var records = response.value;
579+
//do something else with a records array. Access a record: response.value[0].subject;
580+
})
581+
.catch(function (error){
582+
//catch an error
583+
});
584+
```
585+
586+
OR advanced function:
572587

573588
```js
574589
//set the request parameters
575590
var request = {
576591
collection: "leads",
577592
select: ["fullname", "subject"],
578593
filter: "statecode eq 0",
579-
maxPageSize: 5
594+
maxPageSize: 5 //just for an example
580595
};
581596

582597
//perform a multiple records retrieve operation
@@ -714,6 +729,9 @@ dynamicsWebApi.executeFetchXml("accounts", fetchXml).then(function (response) {
714729
});
715730
```
716731

732+
Starting from version 1.2.5 DynamicsWebApi has an alias with a shorter name and same parameters: `dynamicsWebApi.fetch(...)`,
733+
that works in the same way as `executeFetchXml`.
734+
717735
#### Paging
718736

719737
```js
@@ -748,6 +766,28 @@ dynamicsWebApi.executeFetchXml("accounts", fetchXml).then(function (response) {
748766
//catch...
749767
```
750768

769+
#### Fetch All records
770+
771+
The following function executes a FetchXml and goes through all pages automatically:
772+
773+
```js
774+
var fetchXml = '<fetch mapping="logical">' +
775+
'<entity name="account">' +
776+
'<attribute name="accountid"/>' +
777+
'<attribute name="name"/>' +
778+
'</entity>' +
779+
'</fetch>';
780+
781+
dynamicsWebApi.executeFetchXmlAll("accounts", fetchXml).then(function (response) {
782+
783+
//do something with results here; access records response.value[0].accountid
784+
})
785+
//catch...
786+
```
787+
788+
Starting from version 1.2.5 DynamicsWebApi has an alias with a shorter name and same parameters: `dynamicsWebApi.fetchAll(...)`,
789+
that works in the same way as `executeFetchXmlAll`.
790+
751791
### Execute Web API functions
752792

753793
#### Bound functions
@@ -822,7 +862,7 @@ dynamicsWebApi.executeUnboundAction("WinOpportunity", actionRequest).then(functi
822862
### In Progress
823863

824864
- [ ] overloaded functions with rich request options for all Web API operations.
825-
- [ ] get all pages requests, such as: countAll, retrieveMultipleAll, fetchXmlAll and etc.
865+
- [X] get all pages requests, such as: countAll, retrieveMultipleAll, fetchXmlAll and etc. Implemented in v.1.2.5.
826866
- [ ] "formatted" values in responses. For instance: Web API splits information about lookup fields into separate properties, the config option "formatted" will enable developers to retrieve all information about such fields in a single requests and access it through DynamicsWebApi custom response objects.
827867
- [ ] Intellisense for request objects.
828868

lib/dynamics-web-api-callbacks.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,23 @@ function DynamicsWebApi(config) {
652652
}, successCallback, errorCallback, nextPageLink);
653653
}
654654

655+
/**
656+
* Sends an asynchronous request to retrieve all records.
657+
*
658+
* @param {string} collection - The Name of the Entity Collection.
659+
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
660+
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
661+
* @param {Array} [select] - Use the $select system query option to limit the properties returned.
662+
* @param {string} [filter] - Use the $filter system query option to set criteria for which entities will be returned.
663+
*/
664+
this.retrieveAll = function (collection, successCallback, errorCallback, select, filter) {
665+
return _retrieveAllRequest({
666+
collection: collection,
667+
select: select,
668+
filter: filter
669+
}, successCallback, errorCallback);
670+
}
671+
655672
/**
656673
* Sends an asynchronous request to retrieve records.
657674
*
@@ -741,7 +758,7 @@ function DynamicsWebApi(config) {
741758
* @param {string} [pagingCookie] - Paging cookie. For retrieving the first page, pagingCookie should be null.
742759
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
743760
*/
744-
this.executeFetchXml = function (collection, fetchXml, successCallback, errorCallback, includeAnnotations, pageNumber, pagingCookie, impersonateUserId) {
761+
var executeFetchXml = function (collection, fetchXml, successCallback, errorCallback, includeAnnotations, pageNumber, pagingCookie, impersonateUserId) {
745762

746763
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.executeFetchXml", "collection");
747764
ErrorHelper.stringParameterCheck(fetchXml, "DynamicsWebApi.executeFetchXml", "fetchXml");
@@ -796,6 +813,39 @@ function DynamicsWebApi(config) {
796813
_sendRequest("GET", result.url + "?fetchXml=" + encodedFetchXml, null, result.headers, onSuccess, errorCallback);
797814
}
798815

816+
this.fetch = this.executeFetchXml = executeFetchXml;
817+
818+
var _executeFetchXmlAll = function (collection, fetchXml, successCallback, errorCallback, includeAnnotations, pageNumber, pagingCookie, impersonateUserId, records) {
819+
var records = records || [];
820+
821+
var internalSuccessCallback = function (response) {
822+
records = records.concat(response.value);
823+
824+
if (response.PagingInfo) {
825+
_executeFetchXmlAll(collection, fetchXml, successCallback, errorCallback, includeAnnotations, response.PagingInfo.nextPage, response.PagingInfo.cookie, impersonateUserId, records);
826+
}
827+
else {
828+
successCallback({ value: records });
829+
}
830+
};
831+
832+
executeFetchXml(collection, fetchXml, internalSuccessCallback, errorCallback, includeAnnotations, pageNumber, pagingCookie, impersonateUserId);
833+
}
834+
835+
/**
836+
* Sends an asynchronous request to execute FetchXml to retrieve all records.
837+
*
838+
* @param {string} collection - An object that represents all possible options for a current request.
839+
* @param {string} fetchXml - FetchXML is a proprietary query language that provides capabilities to perform aggregation.
840+
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
841+
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
842+
* @param {string} [includeAnnotations] - Use this parameter to include annotations to a result. For example: * or Microsoft.Dynamics.CRM.fetchxmlpagingcookie
843+
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
844+
*/
845+
this.fetchAll = this.executeFetchXmlAll = function (collection, fetchXml, successCallback, errorCallback, includeAnnotations, impersonateUserId) {
846+
return _executeFetchXmlAll(collection, fetchXml, successCallback, errorCallback, includeAnnotations, null, null, impersonateUserId);
847+
}
848+
799849
/**
800850
* Associate for a collection-valued navigation property. (1:N or N:N)
801851
*

lib/dynamics-web-api.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ function DynamicsWebApi(config) {
630630
* @returns {Promise}
631631
*/
632632
this.countAll = function (collection, filter, select) {
633-
return this.retrieveAllRequest({
633+
return _retrieveAllRequest({
634634
collection: collection,
635635
filter: filter,
636636
select: select
@@ -660,7 +660,23 @@ function DynamicsWebApi(config) {
660660
}
661661

662662
/**
663-
* Sends an asynchronous request to count records. Returns: DWA.Types.FetchXmlResponse
663+
* Sends an asynchronous request to retrieve all records.
664+
*
665+
* @param {string} collection - The Name of the Entity Collection.
666+
* @param {Array} [select] - Use the $select system query option to limit the properties returned.
667+
* @param {string} [filter] - Use the $filter system query option to set criteria for which entities will be returned.
668+
* @returns {Promise}
669+
*/
670+
this.retrieveAll = function (collection, select, filter) {
671+
return _retrieveAllRequest({
672+
collection: collection,
673+
select: select,
674+
filter: filter
675+
});
676+
}
677+
678+
/**
679+
* Sends an asynchronous request to execute FetchXml to retrieve records. Returns: DWA.Types.FetchXmlResponse
664680
*
665681
* @param {string} collection - An object that represents all possible options for a current request.
666682
* @param {string} fetchXml - FetchXML is a proprietary query language that provides capabilities to perform aggregation.
@@ -670,14 +686,12 @@ function DynamicsWebApi(config) {
670686
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
671687
* @returns {Promise}
672688
*/
673-
this.executeFetchXml = function (collection, fetchXml, includeAnnotations, pageNumber, pagingCookie, impersonateUserId) {
689+
var executeFetchXml = function (collection, fetchXml, includeAnnotations, pageNumber, pagingCookie, impersonateUserId) {
674690

675691
ErrorHelper.stringParameterCheck(collection, "DynamicsWebApi.executeFetchXml", "type");
676692
ErrorHelper.stringParameterCheck(fetchXml, "DynamicsWebApi.executeFetchXml", "fetchXml");
677693

678-
if (pageNumber == null) {
679-
pageNumber = 1;
680-
}
694+
pageNumber = pageNumber || 1;
681695

682696
ErrorHelper.numberParameterCheck(pageNumber, "DynamicsWebApi.executeFetchXml", "pageNumber");
683697
var replacementString = '$1 page="' + pageNumber + '"';
@@ -723,6 +737,46 @@ function DynamicsWebApi(config) {
723737
});
724738
}
725739

740+
/**
741+
* Sends an asynchronous request to execute FetchXml to retrieve records. Returns: DWA.Types.FetchXmlResponse
742+
*
743+
* @param {string} collection - An object that represents all possible options for a current request.
744+
* @param {string} fetchXml - FetchXML is a proprietary query language that provides capabilities to perform aggregation.
745+
* @param {string} [includeAnnotations] - Use this parameter to include annotations to a result. For example: * or Microsoft.Dynamics.CRM.fetchxmlpagingcookie
746+
* @param {number} [pageNumber] - Page number.
747+
* @param {string} [pagingCookie] - Paging cookie. For retrieving the first page, pagingCookie should be null.
748+
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
749+
* @returns {Promise}
750+
*/
751+
this.fetch = this.executeFetchXml = executeFetchXml;
752+
753+
var _executeFetchXmlAll = function (collection, fetchXml, includeAnnotations, pageNumber, pagingCookie, impersonateUserId, records) {
754+
var records = records || [];
755+
756+
return executeFetchXml(collection, fetchXml, includeAnnotations, pageNumber, pagingCookie, impersonateUserId, records).then(function (response) {
757+
records = records.concat(response.value);
758+
759+
if (response.PagingInfo) {
760+
return _executeFetchXmlAll(collection, fetchXml, includeAnnotations, response.PagingInfo.nextPage, response.PagingInfo.cookie, impersonateUserId, records);
761+
}
762+
763+
return { value: records };
764+
});
765+
}
766+
767+
/**
768+
* Sends an asynchronous request to execute FetchXml to retrieve all records.
769+
*
770+
* @param {string} collection - An object that represents all possible options for a current request.
771+
* @param {string} fetchXml - FetchXML is a proprietary query language that provides capabilities to perform aggregation.
772+
* @param {string} [includeAnnotations] - Use this parameter to include annotations to a result. For example: * or Microsoft.Dynamics.CRM.fetchxmlpagingcookie
773+
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
774+
* @returns {Promise}
775+
*/
776+
this.fetchAll = this.executeFetchXmlAll = function (collection, fetchXml, includeAnnotations, impersonateUserId) {
777+
return _executeFetchXmlAll(collection, fetchXml, includeAnnotations, null, null, impersonateUserId);
778+
}
779+
726780
/**
727781
* Associate for a collection-valued navigation property. (1:N or N:N)
728782
*

0 commit comments

Comments
 (0)