Introduction:

Here we will discuss how we can create a SharePoint list using REST API as well as how we can delete list using REST API in SharePoint Online Office 365. The same REST API code will also work fine in SharePoint 2013 and SharePoint 2016.

Here we will put our HTML and REST API code inside a script editor web part which we will add inside a web part page.

Create List using REST API SharePoint Online:

First, let us discuss how we can create a list using REST API in SharePoint online. Here let us take a textbox and a button. We will let the user put list name in the textbox and click on the Submit button which will create the list and give a successful message.

Below is the HTML code.

HTML Code:

<div>
<strong>Enter List Name::</strong>
<input type="text" id="txtListName" />
<input type="button" id="btnSubmitListName" value="Submit" />
</div>
<div id="divCreateListResults"></div>

REST API code:

Below is the REST API code. Here we are calling the createList() method. In the metadata, we are passing the list name, base list as well as metadata.

<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmitListName").on("click", function () {
var listName = $("#txtListName").val();
createList(listName);
});
}
function createList(listName) {
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists";
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.List' },
'BaseTemplate': 100,
'Title': listName
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
}
function onQuerySucceeded(data) {
$("#divCreateListResults").html(data.d.Title + " successfully created!");
}
function onQueryFailed() {
alert('Error!');
}
</script>

Once you save the page, the form will look like below where the user can put the list name and click Submit which will create the list.

If you go to the Site Contents page, you can see the list created like below:

Delete List using REST API in SharePoint Online Office 365:

Here we will discuss how we can delete the list using REST API in SharePoint Online Office 365. Let us use a textbox and a button. The user will enter list name in the textbox and click the button which will delete the list.

Here we will add the HTML and REST API code inside a script editor web part which is there inside a web part page.

HTML Code:

<div>
<strong>Enter the name of the list to delete:</strong>
<input type="text" id="txtListName" />
<input type="button" id="btnSubmitListName" value="Submit" />
</div>
<div id="divDeleteListResults"></div>

REST API Code:

<script>
$(function () {
bindButtonClick();
});
 
function bindButtonClick() {
$("#btnSubmitListName").on("click", function () {
var listName = $("#txtListName").val();
deleteList(listName);
});
}
function deleteList(listName) {
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('" + listName + "')";
$.ajax({
url: fullUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
success: onQuerySucceeded,
error: onQueryFailed
});
}
function onQuerySucceeded() {
$("#divDeleteListResults").html("List successfully deleted!");
}
function onQueryFailed(sender, args) {
alert('Error!');
}
</script>

One you save and refresh the page, the HTML will appear like below and once user give the list name and click Submit, it will delete the list.

Now when you go and see the Site Contents page, the list will not be there.

References:

You can also visit few useful articles:

Conclusion:

Here we have discussed how we can create and delete a list using REST API in SharePoint Online Office 365.