SharePoint Online Office 365 Create and Delete List using Rest API

SharePoint 2013 delete list using rest api

This SharePoint Online tutorial, you will get to know how to create a list using Rest API in SharePoint 2013/2016 or SharePoint Online. Also, delete SharePoint list using Rest API in SharePoint 2013/2016 or SharePoint Online. The same Rest API code will also work fine in SharePoint 2013 and SharePoint 2016 to create and delete list.


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 SharePoint Online.

If you new to HTML form design then read: Learn HTML and Design your first HTML form (feedback form) in less than 10 minutes.

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 src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<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>

Here once you Save the page, the form will look like below where user can put the list name and click on submit which will create the list.

how to create list using rest api sharepoint 2013?
how to create list using rest api sharepoint 2013?

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

SharePoint 2013 delete list using rest api
SharePoint 2013 delete list using rest api

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. Here let us take a textbox and a button. The user will enter list name in the textbox and click on 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 src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<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 the page and refresh the page, the html will appear like below and once user give the list name and click on Submit, it will delete the list.

create list using rest api sharepoint Online Office 365
create list using rest api sharepoint Online Office 365

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

You may like following SharePoint Rest API tutorials:

Conclusion:



Here we have discussed how we can create and delete a list using rest api in SharePoint online Office 365.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →