SharePoint Online Insert Item to SharePoint list using Rest API

SharePoint Online Insert Item to SharePoint list using Rest API

Here we will discuss how we can insert an item to SharePoint list using Rest API in SharePoint Online Office 365. Here I have a SharePoint list name as “MyCompanyInfo” which has two columns known as Title and Industry. In this particular example let us take two textboxes, one for Title and one for Industry and one Submit button. On click on the Submit button, the item will be added in the SharePoint online list.


All the HTML code and Rest API code we will put inside a script editor web part inside a SharePoint web part page.

Also you can check out few useful articles below:
Create SharePoint Site using Rest API in SharePoint Online Office 365

Configure workflow manager for SharePoint 2016 workflow in windows server 2012 r2

Start a task process workflow action Nintex workflow for office 365 SharePoint Online

HTML Code:

<div id=”AddListData”>

<div>

Title:

<br />

<input type=”text” id=”txtTitle” />

</div>

<div>

Industry:

<br />

<input type=”text” id=”txtIndustry” />

</div>

<br />

<div>

<input id=”btnSubmit” type=”button” value=”Submit” />

</div>

</div>

<div id=”divResult”></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() {

$(“#btnSubmit”).on(“click”, function () {

addListItem();

});

}

function addListItem() {

var title = $(“#txtTitle”).val();

var industry = $(“#txtIndustry”).val();

var siteUrl = _spPageContextInfo.webAbsoluteUrl;

var fullUrl = siteUrl + “/_api/web/lists/GetByTitle(‘MyCompanyInfo’)/items”;

$.ajax({

url: fullUrl,

type: “POST”,

data: JSON.stringify({

‘__metadata’: { ‘type’: ‘SP.Data.MyCompanyInfoListItem’ },

‘Title’: title,

‘Industry’: industry

}),

headers: {

“accept”: “application/json;odata=verbose”,

“content-type”: “application/json;odata=verbose”,

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()

},

success: onQuerySucceeded,

error: onQueryFailed

});

}

function onQuerySucceeded(sender, args) {

$(“#divResult”).html(“Item successfully added!”);

}

function onQueryFailed() {

alert(‘Error!’);

}

</script>

Once we Save the page, the form will appear like below and user can put title and industry and it will display successful message like below:

SharePoint Online Insert Item to SharePoint list using Rest API
SharePoint Online Insert Item to SharePoint list using Rest API

Now if you will look at the list, you can see the item added to the SharePoint list.

SharePoint Online Insert Item to SharePoint list using Rest API
SharePoint Online Insert Item to SharePoint list using Rest API

Here we have checked how we can add an item to a SharePoint list using Rest API.



Hope this will be helpful.

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 →