Skip to content

Commit

Permalink
Enhanced the update method to include an option to check the etag value.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjandatta committed Oct 25, 2024
1 parent 65f39b6 commit 1927609
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/gd-sprest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gd-sprest.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gd-sprest",
"version": "8.3.0",
"version": "8.3.1",
"description": "An easy way to develop against the SharePoint REST API.",
"author": "Gunjan Datta <[email protected]> (https://gunjandatta.github.io)",
"license": "MIT",
Expand All @@ -24,7 +24,7 @@
},
"homepage": "https://dattabase.com",
"dependencies": {
"gd-sprest-def": "^1.5.2"
"gd-sprest-def": "^1.5.3"
},
"devDependencies": {
"@babel/core": "^7.18.10",
Expand Down
28 changes: 16 additions & 12 deletions src/helper/listForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { IODataQuery, SP } from "gd-sprest-def";
import { ContextInfo, SPTypes, Web } from "..";
import { Types } from "../../@types";
import { ITargetInfoProps } from "../../@types/utils";

/**
* List Form
Expand Down Expand Up @@ -585,13 +586,18 @@ export const ListForm: IListForm = {
},

// Method to save a new or existing item
saveItem: (info: IListFormResult, formValues: any = {}): PromiseLike<IListFormResult> => {
saveItem: (info: IListFormResult, formValues: any = {}, checkItemVersion?: boolean): PromiseLike<IListFormResult> => {
// Return a promise
return new Promise((resolve, reject) => {
// See if this is an existing item
if (info.item && info.item.update) {
// Set the request properties if we are checking the item version
let requestProps: ITargetInfoProps = checkItemVersion && info.item.etag ? {
requestHeader: { "IF-MATCH": info.item.etag }
} : null;

// Update the item
info.item.update(formValues).execute(response => {
Web(info.webUrl, requestProps).Lists(info.list.Title).Items(info.item.Id).update(formValues).execute(response => {
// Refresh the item
ListForm.refreshItem(info).then(info => {
// Resolve the promise
Expand All @@ -603,18 +609,16 @@ export const ListForm: IListForm = {
formValues["__metadata"] = { type: info.list.ListItemEntityTypeFullName };

// Add the item
info.list.Items().add(formValues)
// Execute the request
.execute(item => {
// Update the info
info.item = item;
info.list.Items().add(formValues).execute(item => {
// Update the info
info.item = item;

// Refresh the item
ListForm.refreshItem(info).then(info => {
// Resolve the promise
resolve(info);
}, reject);
// Refresh the item
ListForm.refreshItem(info).then(info => {
// Resolve the promise
resolve(info);
}, reject);
}, reject);
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IREST } from "../@types";
* SharePoint REST Library
*/
export const $REST: IREST = {
__ver: 8.30,
__ver: 8.31,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
Apps: Lib.Apps,
ContextInfo: Lib.ContextInfo,
Expand Down
11 changes: 9 additions & 2 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,15 @@ export const Request = {
for (var key in data) {
let value = data[key];

// Skip properties
if (key == "__metadata" || key == "results") { continue; }
// Skip the results
if (key == "results") { continue; }

// See if this is the metadata
if (key == "__metadata") {
// Set the etag value and continue
base["etag"] = value["etag"];
continue;
}

// See if the base is a collection property
if (value && value.__deferred && value.__deferred.uri) {
Expand Down
1 change: 0 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ v2.sites.getList("Test").items().execute(items => {
items.results[0].fields();
});

v2.sites().lists("").items();
v2.sites().lists("").items();
v2.sites().lists("293874-239478-238479-32847987").execute(list => {
list.items().execute(items => {
Expand Down

0 comments on commit 1927609

Please sign in to comment.