SharePoint Online Office 365 Create and Delete List using Rest API

SharePoint 2013 delete list using rest api
InfoPath alternatives for form designing SharePoint

Introduction:
Here we will discuss how we can create a SharePoint list using Rest API ad 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 user put list name in the textbox and click on the Submit button which will create the list and give a successful message.

You can also see few useful articles:
– How to delete file from document library using JavaScript object model jsom in SharePoint Online Office 365?

– Create sub site and Page using JavaScript jQuery in SharePoint 2016 or SharePoint 2013 or SharePoint Online

– Show External Content Type list using Business Data Web part in SharePoint 2013

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. User will enter listname 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 Site Contents page, the list will not be there.

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

Similar SharePoint 2013 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 →

Leave a Reply