Last active
March 22, 2024 06:47
-
-
Save huzefamehidpurwala/a30fc06eeac2675aede7f87db8c446d7 to your computer and use it in GitHub Desktop.
crud operations on sharepoint list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
export const customIsEmpty = (val: any): boolean => | |
val === "" || | |
val === null || | |
// val === "null" || | |
// val === "undefined" || | |
val === undefined || | |
(typeof val === "object" && !Object.keys(val).length); | |
export enum CRUD { | |
Create, | |
Read, | |
Update, | |
Delete, | |
} | |
export default function crud( | |
siteAbsoluteUrl: string, | |
apiGetType: string, | |
listname: string, | |
queryApiArgs?: string, // like filter, select etc, to append to the queryURL | |
addToHeaders?: RequestInit, // obj to add to the headers | |
method = CRUD.Read, | |
requestDigest?: string, // requied in create, edit and delete | |
id?: number, // required in edit and delete | |
requestdata?: any // required in edit and delete | |
): Promise<any> { | |
const baseUrl = `${siteAbsoluteUrl}/_api/web/lists/${apiGetType}('${listname}')`; | |
let attachedObj: RequestInit; | |
let queryUrl = ""; | |
switch (method) { | |
case CRUD.Update: { | |
queryUrl = baseUrl + `/Items(${id})`; | |
attachedObj = { | |
method: "POST", | |
credentials: "same-origin", | |
headers: { | |
Accept: "application/json", | |
"Content-Type": "application/json;odata=verbose", | |
"X-RequestDigest": requestDigest || "", | |
"IF-MATCH": "*", | |
"X-HTTP-Method": "MERGE", | |
}, | |
body: requestdata, | |
}; | |
break; | |
} | |
case CRUD.Delete: { | |
queryUrl = baseUrl + `/Items(${id})`; | |
attachedObj = { | |
method: "POST", | |
credentials: "same-origin", | |
headers: { | |
Accept: "application/json", | |
"If-Match": "*", | |
"X-HTTP-Method": "DELETE", | |
"X-RequestDigest": requestDigest || "", | |
}, | |
}; | |
break; | |
} | |
case CRUD.Create: { | |
queryUrl = baseUrl + "/Items"; | |
attachedObj = { | |
method: "POST", | |
credentials: "same-origin", | |
headers: { | |
Accept: "application/json", | |
"Content-Type": "application/json", | |
"X-RequestDigest": requestDigest || "", | |
}, | |
body: requestdata, | |
}; | |
break; | |
} | |
case CRUD.Read: | |
default: { | |
queryUrl = baseUrl + `/Items`; | |
attachedObj = { headers: { Accept: "application/json" } }; | |
break; | |
} | |
} | |
return new Promise<any>((resolve, reject) => { | |
const conditionToCheckIfEmptyId = | |
method !== CRUD.Read && method !== CRUD.Create && customIsEmpty(id); | |
if (conditionToCheckIfEmptyId) reject({ status: 500 }); | |
if (queryApiArgs) queryUrl = queryUrl + `?${queryApiArgs}`; | |
try { | |
fetch(queryUrl, { ...attachedObj, ...addToHeaders }) | |
// .then((res) => res.json()) | |
.then((data) => resolve(data)) | |
.catch((error) => reject(error)); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} | |
// * Examples | |
/* crud(this.props.domain, "GetByTitle", this.props.listName, "$select=*,Author/Title&$expand=Author") | |
.then((res) => res.json()) | |
.then( | |
(data) => | |
data.value && this.setState({ data: data.value, loading: false }) | |
) | |
.catch((err) => console.error("errro", err)); */ | |
/* crud( | |
domain, | |
"GetByTitle", | |
listName, | |
"", | |
undefined, | |
CRUD.Delete, | |
digest, | |
selectionDetails.Id | |
) | |
.then((res) => res.json()) | |
.then((data) => { | |
console.log("success from api update", data); | |
updateLoading(); | |
}) | |
.catch((err) => | |
console.error("error in delete api", err) | |
); */ | |
/* crud( | |
domain, | |
"GetByTitle", | |
listName, | |
"", | |
undefined, | |
hideDialog !== strings.New | |
? hideDialog === strings.Edit | |
? CRUD.Update | |
: undefined | |
: CRUD.Create, | |
digest, | |
selectionDetails.Id || undefined, | |
JSON.stringify(selectionDetails) | |
) | |
.then((res) => { | |
console.log("success from api update", res); | |
updateLoading(); | |
}) | |
.catch((err) => | |
console.error("error in update api", err) | |
); */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment