Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit eb96952

Browse files
update to items.add to allow passing of the ListItemEntityTypeName to save on calls when adding multiple items
1 parent 8fdde54 commit eb96952

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/sharepoint/items.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Util } from "../utils/util";
88
import * as Types from "./types";
99
import { ODataParserBase } from "./odata";
1010
import { AttachmentFiles } from "./attachmentfiles";
11+
import { List } from "./lists";
1112

1213
/**
1314
* Describes a collection of Item objects
@@ -58,29 +59,42 @@ export class Items extends QueryableCollection {
5859
*
5960
* @param properties The new items's properties
6061
*/
61-
public add(properties: TypedHash<any> = {}): Promise<ItemAddResult> {
62+
public add(properties: TypedHash<any> = {}, listItemEntityTypeFullName: string = null): Promise<ItemAddResult> {
6263

63-
let removeDependency = this.addBatchDependency();
64-
65-
let parentList = this.getParent(QueryableInstance);
66-
67-
return parentList.select("ListItemEntityTypeFullName").getAs<{ ListItemEntityTypeFullName: string }>().then((d) => {
64+
let doAdd = (listItemEntityType: string): Promise<ItemAddResult> => {
6865

6966
let postBody = JSON.stringify(Util.extend({
70-
"__metadata": { "type": d.ListItemEntityTypeFullName },
67+
"__metadata": { "type": listItemEntityType },
7168
}, properties));
7269

73-
let promise = this.postAs<{ Id: number }>({ body: postBody }).then((data) => {
70+
return this.postAs<{ Id: number }>({ body: postBody }).then((data) => {
7471
return {
7572
data: data,
7673
item: this.getById(data.Id),
7774
};
7875
});
7976

80-
removeDependency();
77+
}
8178

82-
return promise;
83-
});
79+
if (!listItemEntityTypeFullName) {
80+
81+
let parentList = this.getParent(List);
82+
83+
let removeDependency = this.addBatchDependency();
84+
85+
return parentList.getListItemEntityTypeFullName().then(n => {
86+
87+
let promise = doAdd(n);
88+
89+
removeDependency();
90+
91+
return promise;
92+
});
93+
94+
} else {
95+
96+
return doAdd(listItemEntityTypeFullName);
97+
}
8498
}
8599
}
86100

src/sharepoint/lists.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,15 @@ export class List extends QueryableSecurable {
422422
}
423423
});
424424
}
425+
426+
/**
427+
* Returns the ListItemEntityTypeFullName for this list, used when adding/updating list items
428+
*
429+
*/
430+
public getListItemEntityTypeFullName(): Promise<string> {
431+
432+
return this.select("ListItemEntityTypeFullName").getAs<{ ListItemEntityTypeFullName: string }>().then(o => o.ListItemEntityTypeFullName);
433+
}
425434
}
426435

427436
export interface ListAddResult {

0 commit comments

Comments
 (0)