In this repo contains Sharepoint 2013/2016/2019/Online, Office 365 REST API Code Sample/Example which will using SP Rest utility SPRest.ts
or SPRest.js
Here utility library can be used with TypeScript in #Spfx and also work with most browsers.
As I have used fetch
API which is not available in IE11 browser so you can use polyfill
var util=new SPRest("https://brgrp.sharepoint.com");
// Get All Item from List item
util.Utils.ListItem.GetAllItem({listName:"PlaceHolderList"}).then(function(r){
console.log(r);
// Response received. TODO bind record to table or somewhere else.
});
//Get all selected column Data using full URL
var reqUItemUrl="https://brgrp.sharepoint.com/_api/web/lists/getbytitle('PlaceHolderList')/items";
util.Utils.ListItem
.GetAllItem({
url:reqUItemUrl+"?$select=Id,Title&$top=200"
}).then(function(r){console.log(r);
//Response Received
});
//Get all selected column data with listName and oDataOption
util.Utils.ListItem.GetAllItem(
{"listName":"PlaceHolderList"
,oDataOption:"$select=Id,Title&$top=200"
}).then(function(r){console.log(r);
//Response Received
});
// Get List Item By Id
util.Utils.ListItem.GetItemById({listName:"PlaceHolderList",Id:201}).then(function(r){
console.log(r);
// Response received.
});
// Add ListItem to Sharepoint List
util.Utils.ListItem.Add({listName:"PlaceHolderList"
,data:{Title:"New Item Created For Demo",UserId:1,Completed:"true"}}).then(function(r){
console.log(r);
// Added New List item response received with newly created item
});
// Update List item based on ID with new data in SharePoint List
util.Utils.ListItem.Update({listName:"PlaceHolderList",Id:201
,data:{Title:"Updated List Item",UserId:1,Completed:"true"}}).then(function(r){
// List Item Updated and received response with status 204
console.log(r);
});
// Delete List item based on ID
util.Utils.ListItem.Delete({listName:"PlaceHolderList",Id:201}).then(function(r){
// List Item Deleted and received response with status 200
console.log(r);
});
Reference link : https://www.c-sharpcorner.com/article/easy-sharepoint-listitem-crud-operation-using-rest-api-wrapper/
var getRequestDigest=(rootUrl)=>{
var _payloadOptions = { method: "POST",
headers: { credentials: "include", Accept: "application/json; odata=verbose"
,"Content-Type": "application/json; odata=verbose" }
};
//RequestDigest Request
return fetch(rootUrl+"/_api/contextinfo",_payloadOptions).then(r=>r.json())
}
//Get Digest first then create txt file
getRequestDigest("https://brgrp.sharepoint.com").then (r=>{
//Received Request Digest
var reqUrl="https://brgrp.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')"
fetch(reqUrl+"/Files/add(url='file_name.txt',overwrite=true)",
{method:"POST",headers:
{accept:"application/json;odata=verbose",
"Content-Type":"application/json;odata=verbose","X-RequestDigest":r.d.GetContextWebInformation.FormDigestValue }
,body:"Content Of Text File"}).then(r=>console.log(r))
})
//payload for request
body= {"formValues":[{"FieldName":"Title","FieldValue":"Single Update Title with versioning__"}]
,bNewDocumentUpdate:true}
//Header data for sharepoint POST Request
var _payloadOptions = {
method: "POST",
body: undefined,
headers: {
credentials: "include",
Accept: "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose"
}
};
//Get RequestDigest First
fetch("https://brgrp.sharepoint.com/_api/contextinfo",_payloadOptions).then(r=>r.json())
.then(r=>
{
_payloadOptions.headers["X-RequestDigest"]=r.d.GetContextWebInformation.FormDigestValue
_payloadOptions.body=JSON.stringify(body);
// Make REST API Call to update list item without increamenting version.
fetch("https://brgrp.sharepoint.com/_api/web/Lists/GetbyTitle('Documents')/items(1)/ValidateUpdateListItem()",
_payloadOptions).then(r=>r.json()).then(r=>console.log(r))
});
Reference link : https://www.c-sharpcorner.com/article/update-a-sharepoint-list-item-without-increasing-its-item-file-version-using-res/
BatchUtils can be found in Here
Here rootUrl required to Generate Request Digest Token as batch Request is POST request.
var arr=["https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(212)"
, "https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(213)"
, "https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(214)"];
BatchUtils.GetBatchAll({rootUrl:"https://brgrp.sharepoint.com",
batchUrls:arr}).then(r=>console.log(r))
You can skip rootUrl if you have already generated request digest as below.
var arr=["https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(212)"
, "https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(213)"
, "https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(214)"];
getRequestDigest("https://brgrp.sharepoint.com").then(r=>{
BatchUtils.GetBatchAll({rootUrl:"https://brgrp.sharepoint.com",
batchUrls:arr,FormDigestValue: r.d.GetContextWebInformation.FormDigestValue}).then(r=>console.log(r))
});
SharePoint Batch API is very powerful and useful for making multiple request to single request. This BatchUtils Support ADD/UPDATE/DELETE Operation, it can be combined in single batch Requests.
// Prepare collection of request with requestUrl and payload data.
var arr=[{
reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(212)"
,action:"UPDATE",
data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_1"}},
{
reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(213)"
,action:"UPDATE",
data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_2"}},
{
reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(214)"
,action:"UPDATE",
data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_3"}},
{
reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"
,action:"ADD",
data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_1"}}
,{
reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(215)"
,action:"DELETE"}];
getRequestDigest().then(r=>{
BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",
batchUrls:arr,FormDigestValue: r.d.GetContextWebInformation.FormDigestValue}).then(r=>console.log(r))
})