Skip to content

Commit 4c75087

Browse files
add trim for Prefer header items, update readme
1 parent 3380c80 commit 4c75087

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ id | String | `retrieveRequest`, `updateRequest`, `upsertRequest`, `deleteReques
183183
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).
184184
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).
185185
impersonate | String | All | A String representing the GUID value for the Dynamics 365 system user id. Impersonates the user.
186-
includeAnnotations | String | `retrieveRequest`, `retrieveMultipleRequest` | 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.
186+
includeAnnotations | String | `retrieveRequest`, `retrieveMultipleRequest`, `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.
187187
maxPageSize | Number | `retrieveMultipleRequest` | Sets the odata.maxpagesize preference value to request the number of entities returned in the response.
188188
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.
189189
orderBy | Array | `retrieveMultipleRequest` | 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.
@@ -229,6 +229,35 @@ dynamicsWebApi.create(lead, "leads").then(function (id) {
229229
})
230230
```
231231

232+
If you need to return just created entity record, add "return=representation" parameter:
233+
234+
```js
235+
//initialize a CRM entity record object
236+
var lead = {
237+
subject: "Test WebAPI",
238+
firstname: "Test",
239+
lastname: "WebAPI",
240+
jobtitle: "Title"
241+
};
242+
//call dynamicsWebApi.create function
243+
dynamicsWebApi.create(lead, "leads", ["return=representation"]).then(function (record) {
244+
//do something with a record here
245+
var subject = record.subject;
246+
}).catch(function (error) {
247+
//catch error here
248+
})
249+
```
250+
251+
Also you can include attribute annotations:
252+
253+
```js
254+
dynamicsWebApi.create(lead, "leads", ["return=representation", "odata.include-annotations=*"]) //...
255+
//or
256+
dynamicsWebApi.create(lead, "leads", "return=representation,odata.include-annotations=*") //...
257+
//and select some attributes from the record
258+
dynamicsWebApi.create(lead, "leads", ["return=representation", "odata.include-annotations=*"], ["subject"]) //...
259+
```
260+
232261
### Update a record
233262

234263
#### Basic

lib/utilities/buildPreferHeader.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ module.exports = function buildPreferHeader(request, functionName, config) {
2020
prefer = prefer.split(',');
2121
}
2222
for (var i in prefer) {
23-
if (prefer[i] === DWA.Prefer.ReturnRepresentation) {
23+
var item = prefer[i].trim();
24+
if (item === DWA.Prefer.ReturnRepresentation) {
2425
returnRepresentation = true;
2526
}
26-
else if (prefer[i].startsWith("odata.include-annotations=")) {
27-
includeAnnotations = prefer[i].replace('odata.include-annotations=', '').replace(/"/g,'');
27+
else if (item.startsWith("odata.include-annotations=")) {
28+
includeAnnotations = item.replace('odata.include-annotations=', '').replace(/"/g,'');
2829
}
29-
else if (prefer[i].startsWith("odata.maxpagesize=")) {
30-
maxPageSize = prefer[i].replace('odata.maxpagesize=', '').replace(/"/g, '');
30+
else if (item.startsWith("odata.maxpagesize=")) {
31+
maxPageSize = item.replace('odata.maxpagesize=', '').replace(/"/g, '');
3132
}
3233
}
3334
}

tests/common-tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,19 @@ describe("RequestConverter.convertRequestOptions -", function() {
931931
});
932932
});
933933

934+
it("prefer - SPACE - includeAnnotations & maxPageSize", function () {
935+
var dwaRequest = {
936+
prefer: 'odata.include-annotations=*, odata.maxpagesize=20'
937+
};
938+
939+
var result = RequestConverter.convertRequestOptions(dwaRequest, "", stubUrl);
940+
expect(result).to.deep.equal({
941+
url: stubUrl, query: "", headers: {
942+
Prefer: 'odata.include-annotations="*",odata.maxpagesize=20'
943+
}
944+
});
945+
});
946+
934947
it("prefer - returnRepresentation & maxPageSize", function () {
935948
var dwaRequest = {
936949
prefer: 'return=representation,odata.maxpagesize=20'

0 commit comments

Comments
 (0)