What is SharePoint Online REST API ?
What is SharePoint Online REST API ?
Cancel

What is SharePoint Online REST API ?

HTTP Verbs : GET, POST, DELETE, MERGE

Understandd SharePoint Online CRUD(Create, Read, Update, Delete) Operation using REST API

Repeated code in CRUD Operation Demo for below code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Example : getListData ("https://brsupport.sharepoint.com/_api/Lists").then(r=>console.log(r))
var getListData = function(reqUrl) {
  //GET Request using Fetch API
  return fetch(reqUrl, {
    method: "GET", //Http Verb
    credentials: "include", //Send Cookie and other credential while make request
    headers: {
      Accept: "application/json; odata=nometadata" // Requesting data in specific this format
      //"Content-Type": "application/json; odata=verbose" //Indicate that we are passing data in this format to server
    } //Headers
    //body: undefined
  }).then(r => r.json());
};


// Example : getRequestDigest("https://brsupport.sharepoint.com").then(r=>console.log(r))
var getRequestDigest = function(rootUrl) {
  //RequestDigest Request using Fetch API
  return fetch(rootUrl + "/_api/contextinfo", {
    method: "POST", //Http Verb
    credentials: "include", //Send Cookie and other credential while make request
    headers: {
      Accept: "application/json; odata=nometadata", // Requesting data in specific this format
      "Content-Type": "application/json; odata=nometadata" //Indicate that we are passing data in this format to server
    }, //Headers
    body: undefined
  }).then(r => r.json());
};

//Example : CreateNewList ("https://brsupport.sharepoint.com/_api/Lists","SPList").then(r=>console.log(r))

var CreateNewList = function(reqUrl, listName) {
  return getRequestDigest(reqUrl.split("/_api")[0]).then(function(digest) {
    console.log("Received Request Digest",digest.FormDigestValue);
    //RequestDigest Request using Fetch API
    return fetch(reqUrl, {
      method: "POST", //Http Verb
      credentials: "include", //Send Cookie and other credential while make request
      headers: {
        Accept: "application/json; odata=nometadata", // Requesting data in specific this format
        "Content-Type": "application/json; odata=nometadata", //Indicate that we are passing data in this format to server
        "X-RequestDigest":digest.FormDigestValue //Requestdigest Required for POST, UPDATE and DELETE Operation
      }, //Headers
      body: JSON.stringify({Title :listName
 , BaseTemplate: 100})
    }).then(r => r.json());
  });
};

//Example : UpdateList ("https://brsupport.sharepoint.com/_api/Lists/GetByTitle('SPList')","SPList1").then(r=>console.log(r))
var UpdateList = function(reqUrl, listName) {
  return getRequestDigest(reqUrl.split("/_api")[0]).then(function(digest) {
    console.log("Received Request Digest",digest.FormDigestValue);
    //RequestDigest Request using Fetch API
    return fetch(reqUrl, {
      method: "POST", //Http Verb
      credentials: "include", //Send Cookie and other credential while make request
      headers: {
        Accept: "application/json; odata=nometadata", // Requesting data in specific this format
        "Content-Type": "application/json; odata=nometadata", //Indicate that we are passing data in this format to server
        "X-RequestDigest":digest.FormDigestValue, //Requestdigest Required for POST, UPDATE and DELETE Operation
    "IF-MATCH": "*",
        "X-HTTP-Method": "MERGE"
      }, //Headers
      body: JSON.stringify({Title :listName
    })}).then(r => r); // Update Operation doesn't return any data so need to convert it to JSON then(r => r.json());
  });
};

//Example : DeleteList ("https://brsupport.sharepoint.com/_api/Lists/GetByTitle('SPList1')").then(r=>console.log(r))
var DeleteList = function(reqUrl) {
  return getRequestDigest(reqUrl.split("/_api")[0]).then(function(digest) {
    console.log("Received Request Digest",digest.FormDigestValue);
    //RequestDigest Request using Fetch API
    return fetch(reqUrl, {
      method: "POST", //Http Verb
      credentials: "include", //Send Cookie and other credential while make request
      headers: {
        Accept: "application/json; odata=nometadata", // Requesting data in specific this format
        "Content-Type": "application/json; odata=nometadata", //Indicate that we are passing data in this format to server
        "X-RequestDigest":digest.FormDigestValue, //Requestdigest Required for POST, UPDATE and DELETE Operation
    "IF-MATCH": "*",
        "X-HTTP-Method": "DELETE"
      }, //Headers
     // body: JSON.stringify({Title :listName //DELETE Operation doesn't need body parameter
 }).then(r => r); // Delete Operation doesn't return any data so need to convert it to JSON then(r => r.json());
  });
};
1
## Sharepoint Online REST API Filters, Select, Sorting using OData queries