Skip to content

Commit

Permalink
changes to allow working with Entity Metadata; fix #23; fix other issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrRogov committed Mar 10, 2018
1 parent 2b40912 commit 5560906
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ entity | Object | `createRequest`, `updateRequest`, `upsertRequest` | A JavaScri
expand | Array | `retrieveRequest`, `createRequest`, `updateRequest`, `upsertRequest` | An array of Expand Objects (described below the table) representing the $expand OData System Query Option value to control which related records are also returned.
filter | String | `retrieveRequest`, `retrieveMultipleRequest`, `retrieveAllRequest` | Use the $filter system query option to set criteria for which entities will be returned.
id | String | `retrieveRequest`, `createRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | `deprecated in v.1.3.4` Use `key` field, instead of `id`. A String representing the Primary Key (GUID) of the record.
ifmatch | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | Sets If-Match header value that enables to use conditional retrieval or optimistic concurrency in applicable requests. [More info](https://msdn.microsoft.com/en-us/library/mt607711.aspx).
ifmatch | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | Sets If-Match header value that enables to use conditional retrieval or optimistic concurrency in applicable requests. [More info](https://msdn.microsoft.com/en-us/library/mt607711.aspx)
ifnonematch | String | `retrieveRequest`, `upsertRequest` | Sets If-None-Match header value that enables to use conditional retrieval in applicable requests. [More info](https://msdn.microsoft.com/en-us/library/mt607711.aspx).
impersonate | String | All | A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
includeAnnotations | String | `retrieveRequest`, `retrieveMultipleRequest`, `retrieveAllRequest`, `createRequest`, `updateRequest`, `upsertRequest` | Sets Prefer header with value "odata.include-annotations=" and the specified annotation. Annotations provide additional information about lookups, options sets and other complex attribute types.
key | String | `retrieveRequest`, `createRequest`, `updateRequest`, `upsertRequest`, `deleteRequest` | `v.1.3.4+` A String representing collection record's Primary Key (GUID) or Alternate Key(s).
maxPageSize | Number | `retrieveMultipleRequest`, `retrieveAllRequest` | Sets the odata.maxpagesize preference value to request the number of entities returned in the response.
mergeLabels | Boolean | `updateRequest` | `v.1.4.2+` **Metadata Update only!** Sets `MSCRM.MergeLabels` header that controls whether to overwrite the existing labels or merge your new label with any existing language labels. [More info](https://msdn.microsoft.com/en-us/library/mt593078.aspx#Update%20entities)
navigationProperty | String | `retrieveRequest` | A String representing the name of a single-valued navigation property. Useful when needed to retrieve information about a related record in a single request.
noCache | Boolean | All | `v.1.4.0+` If set to `true`, DynamicsWebApi adds a request header `Cache-Control: no-cache`. Default value is `false`.
orderBy | Array | `retrieveMultipleRequest`, `retrieveAllRequest` | An Array (of Strings) representing the order in which items are returned using the $orderby system query option. Use the asc or desc suffix to specify ascending or descending order respectively. The default is ascending if the suffix isn't applied.
Expand Down
35 changes: 29 additions & 6 deletions dist/dynamics-web-api-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,18 +479,25 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
}

if (request.entity) {
ErrorHelper.parameterCheck(request.entity, 'DynamicsWebApi.' + functionName, 'request.entity')
ErrorHelper.parameterCheck(request.entity, 'DynamicsWebApi.' + functionName, 'request.entity');


}

if (request.data) {
ErrorHelper.parameterCheck(request.data, 'DynamicsWebApi.' + functionName, 'request.data')
ErrorHelper.parameterCheck(request.data, 'DynamicsWebApi.' + functionName, 'request.data');
}

if (request.noCache) {
ErrorHelper.boolParameterCheck(request.noCache, 'DynamicsWebApi.' + functionName, 'request.noCache');
headers['Cache-Control'] = 'no-cache';
}

if (request.mergeLabels){
ErrorHelper.boolParameterCheck(request.mergeLabels, 'DynamicsWebApi.' + functionName, 'request.mergeLabels');
headers['MSCRM.MergeLabels'] = 'true';
}

if (request.expand && request.expand.length) {
ErrorHelper.stringOrArrayParameterCheck(request.expand, 'DynamicsWebApi.' + functionName, "request.expand");
if (typeof request.expand === 'string') {
Expand Down Expand Up @@ -785,6 +792,13 @@ function stringifyData(data, config) {
}
}
}
else
if (key.startsWith('oData') ||
key.endsWith('_Formatted') ||
key.endsWith('_NavigationProperty') ||
key.endsWith('_LogicalName')) {
value = undefined;
}

return value;
});
Expand Down Expand Up @@ -900,7 +914,9 @@ function _getEntityNames(entityName, config, successCallback, errorCallback) {
}

function _isEntityNameException(entityName) {
var exceptions = ['EntityDefinitions', '$metadata'];
var exceptions = [
'EntityDefinitions', '$metadata', 'RelationshipDefinitions',
'GlobalOptionSetDefinitions', 'ManagedPropertyDefinitions'];

return exceptions.indexOf(entityName) > -1;
}
Expand Down Expand Up @@ -1004,6 +1020,7 @@ if (!String.prototype.endsWith || !String.prototype.startsWith) {
* @property {boolean} noCache - If set to 'true', DynamicsWebApi adds a request header 'Cache-Control: no-cache'. Default value is 'false'.
* @property {string} savedQuery - A String representing the GUID value of the saved query.
* @property {string} userQuery - A String representing the GUID value of the user query.
* @property {boolean} mergeLabels - If set to 'true', DynamicsWebApi adds a request header 'MSCRM.MergeLabels: true'. Default value is 'false'
*/

/**
Expand Down Expand Up @@ -1221,7 +1238,10 @@ function DynamicsWebApi(config) {
}
};

_makeRequest("PATCH", request, 'update', onSuccess, onError);
//EntityDefinitions cannot be updated using "PATCH" method
var method = request.collection.indexOf('EntityDefinitions') > -1 ? 'PUT' : 'PATCH';

_makeRequest(method, request, 'update', onSuccess, onError);
}

/**
Expand Down Expand Up @@ -2001,7 +2021,7 @@ function DynamicsWebApi(config) {
* Executes an unbound Web API action (not bound to a particular entity record)
*
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
Expand All @@ -2016,7 +2036,7 @@ function DynamicsWebApi(config) {
* @param {string} id - A String representing the GUID value for the record.
* @param {string} collection - The name of the Entity Collection or Entity Logical name.
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
Expand Down Expand Up @@ -2146,6 +2166,7 @@ function parseBatchResponse(response) {

function populateFormattedValues(object) {
var keys = Object.keys(object);
//object._dwa_extendedProperties = [];

for (var i = 0; i < keys.length; i++) {
if (object[keys[i]] != null && object[keys[i]].constructor === Array) {
Expand Down Expand Up @@ -2185,6 +2206,7 @@ function populateFormattedValues(object) {

if (newKey) {
object[newKey] = object[keys[i]];
//object._dwa_extendedProperties.push(newKey);
}
}

Expand Down Expand Up @@ -2289,6 +2311,7 @@ var xhrRequest = function (method, uri, data, additionalHeaders, successCallback
}
}
error.status = request.status;
error.statusText = request.statusText;
errorCallback(error);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/dynamics-web-api-callbacks.min.js

Large diffs are not rendered by default.

35 changes: 29 additions & 6 deletions dist/dynamics-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ function stringifyData(data, config) {
}
}
}
else
if (key.startsWith('oData') ||
key.endsWith('_Formatted') ||
key.endsWith('_NavigationProperty') ||
key.endsWith('_LogicalName')) {
value = undefined;
}

return value;
});
Expand Down Expand Up @@ -656,7 +663,9 @@ function _getEntityNames(entityName, config, successCallback, errorCallback) {
}

function _isEntityNameException(entityName) {
var exceptions = ['EntityDefinitions', '$metadata'];
var exceptions = [
'EntityDefinitions', '$metadata', 'RelationshipDefinitions',
'GlobalOptionSetDefinitions', 'ManagedPropertyDefinitions'];

return exceptions.indexOf(entityName) > -1;
}
Expand Down Expand Up @@ -759,6 +768,7 @@ if (!String.prototype.endsWith || !String.prototype.startsWith) {
* @property {boolean} noCache - If set to 'true', DynamicsWebApi adds a request header 'Cache-Control: no-cache'. Default value is 'false'.
* @property {string} savedQuery - A String representing the GUID value of the saved query.
* @property {string} userQuery - A String representing the GUID value of the user query.
* @property {boolean} mergeLabels - If set to 'true', DynamicsWebApi adds a request header 'MSCRM.MergeLabels: true'. Default value is 'false'
*/

/**
Expand Down Expand Up @@ -1016,9 +1026,12 @@ function DynamicsWebApi(config) {
request.ifmatch = '*'; //to prevent upsert
}

//EntityDefinitions cannot be updated using "PATCH" method
var method = request.collection.indexOf('EntityDefinitions') > -1 ? 'PUT' : 'PATCH';

//copy locally
var ifmatch = request.ifmatch;
return _makeRequest('PATCH', request, 'update')
return _makeRequest(method, request, 'update')
.then(function (response) {
if (response.data) {
return response.data;
Expand Down Expand Up @@ -1643,7 +1656,7 @@ function DynamicsWebApi(config) {
* Executes an unbound Web API action (not bound to a particular entity record)
*
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
* @returns {Promise}
*/
Expand All @@ -1657,7 +1670,7 @@ function DynamicsWebApi(config) {
* @param {string} id - A String representing the GUID value for the record.
* @param {string} collection - The name of the Entity Collection or Entity Logical name.
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
* @returns {Promise}
*/
Expand Down Expand Up @@ -1781,6 +1794,7 @@ function parseBatchResponse(response) {

function populateFormattedValues(object) {
var keys = Object.keys(object);
//object._dwa_extendedProperties = [];

for (var i = 0; i < keys.length; i++) {
if (object[keys[i]] != null && object[keys[i]].constructor === Array) {
Expand Down Expand Up @@ -1820,6 +1834,7 @@ function populateFormattedValues(object) {

if (newKey) {
object[newKey] = object[keys[i]];
//object._dwa_extendedProperties.push(newKey);
}
}

Expand Down Expand Up @@ -1924,6 +1939,7 @@ var xhrRequest = function (method, uri, data, additionalHeaders, successCallback
}
}
error.status = request.status;
error.statusText = request.statusText;
errorCallback(error);
break;
}
Expand Down Expand Up @@ -2081,18 +2097,25 @@ function convertRequestOptions(request, functionName, url, joinSymbol, config) {
}

if (request.entity) {
ErrorHelper.parameterCheck(request.entity, 'DynamicsWebApi.' + functionName, 'request.entity')
ErrorHelper.parameterCheck(request.entity, 'DynamicsWebApi.' + functionName, 'request.entity');


}

if (request.data) {
ErrorHelper.parameterCheck(request.data, 'DynamicsWebApi.' + functionName, 'request.data')
ErrorHelper.parameterCheck(request.data, 'DynamicsWebApi.' + functionName, 'request.data');
}

if (request.noCache) {
ErrorHelper.boolParameterCheck(request.noCache, 'DynamicsWebApi.' + functionName, 'request.noCache');
headers['Cache-Control'] = 'no-cache';
}

if (request.mergeLabels){
ErrorHelper.boolParameterCheck(request.mergeLabels, 'DynamicsWebApi.' + functionName, 'request.mergeLabels');
headers['MSCRM.MergeLabels'] = 'true';
}

if (request.expand && request.expand.length) {
ErrorHelper.stringOrArrayParameterCheck(request.expand, 'DynamicsWebApi.' + functionName, "request.expand");
if (typeof request.expand === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion dist/dynamics-web-api.min.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions lib/dynamics-web-api-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var dwaRequest = function () {
* @property {boolean} noCache - If set to 'true', DynamicsWebApi adds a request header 'Cache-Control: no-cache'. Default value is 'false'.
* @property {string} savedQuery - A String representing the GUID value of the saved query.
* @property {string} userQuery - A String representing the GUID value of the user query.
* @property {boolean} mergeLabels - If set to 'true', DynamicsWebApi adds a request header 'MSCRM.MergeLabels: true'. Default value is 'false'
*/

/**
Expand Down Expand Up @@ -298,7 +299,10 @@ function DynamicsWebApi(config) {
}
};

_makeRequest("PATCH", request, 'update', onSuccess, onError);
//EntityDefinitions cannot be updated using "PATCH" method
var method = request.collection.indexOf('EntityDefinitions') > -1 ? 'PUT' : 'PATCH';

_makeRequest(method, request, 'update', onSuccess, onError);
}

/**
Expand Down Expand Up @@ -1078,7 +1082,7 @@ function DynamicsWebApi(config) {
* Executes an unbound Web API action (not bound to a particular entity record)
*
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
Expand All @@ -1093,7 +1097,7 @@ function DynamicsWebApi(config) {
* @param {string} id - A String representing the GUID value for the record.
* @param {string} collection - The name of the Entity Collection or Entity Logical name.
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {Function} successCallback - The function that will be passed through and be called by a successful response.
* @param {Function} errorCallback - The function that will be passed through and be called by a failed response.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
Expand Down
32 changes: 29 additions & 3 deletions lib/dynamics-web-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var dwaRequest = function () {
* @property {boolean} noCache - If set to 'true', DynamicsWebApi adds a request header 'Cache-Control: no-cache'. Default value is 'false'.
* @property {string} savedQuery - A String representing the GUID value of the saved query.
* @property {string} userQuery - A String representing the GUID value of the user query.
* @property {boolean} mergeLabels - If set to 'true', DynamicsWebApi adds a request header 'MSCRM.MergeLabels: true'. Default value is 'false'
*/

/**
Expand Down Expand Up @@ -324,6 +325,28 @@ function DynamicsWebApi(config) {
return this.retrieveRequest(request);
};

//this.retrieveAttributes = function (entityDefinitionId, attributeType, select, filter) {

// var navigationProperty = 'Attributes';

// if (attributeType) {
// ErrorHelper.stringParameterCheck(attributeType, "DynamicsWebApi.retrieveAttributes", "attributeType");
// navigationProperty += '/' + attributeType;
// }

// var request = {
// collection: 'EntityDefinitions',
// key: entityDefinitionId,
// navigationProperty: 'Attributes',
// select: select,
// filter: filter
// };

// return this.retrieveRequest(request);
//};

//this.retrieveAttribute = function(entityDefinitionId, attributeId, attributeType, select, expand

/**
* Sends an asynchronous request to update a record.
*
Expand All @@ -338,9 +361,12 @@ function DynamicsWebApi(config) {
request.ifmatch = '*'; //to prevent upsert
}

//EntityDefinitions cannot be updated using "PATCH" method
var method = request.collection.indexOf('EntityDefinitions') > -1 ? 'PUT' : 'PATCH';

//copy locally
var ifmatch = request.ifmatch;
return _makeRequest('PATCH', request, 'update')
return _makeRequest(method, request, 'update')
.then(function (response) {
if (response.data) {
return response.data;
Expand Down Expand Up @@ -965,7 +991,7 @@ function DynamicsWebApi(config) {
* Executes an unbound Web API action (not bound to a particular entity record)
*
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
* @returns {Promise}
*/
Expand All @@ -979,7 +1005,7 @@ function DynamicsWebApi(config) {
* @param {string} id - A String representing the GUID value for the record.
* @param {string} collection - The name of the Entity Collection or Entity Logical name.
* @param {string} actionName - The name of the Web API action.
* @param {Object} requestObject - Action request body object.
* @param {Object} [requestObject] - Action request body object.
* @param {string} [impersonateUserId] - A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
* @returns {Promise}
*/
Expand Down
Loading

0 comments on commit 5560906

Please sign in to comment.