CRUD operations in SharePoint 2013 list using Web Services and jQuery

sharepoint 2013 crud operation.jpg

In this article we will explore SharePoint functionality via Web Services. The goal of this article is to provide how to perform basic create, read, update, and delete (CRUD) operations on lists and list items with the SharePoint Web services.

Read some SharePoint tutorials below:

Now, I will demo all the operations on list items including retrieve, create, update and delete onlist items.

sharepoint 2013 crud operation
sharepoint 2013 crud operation

Retrieve list items using Web Services and jQuery In SharePoint 2013:

sharepoint 2013 crud operation using web services
sharepoint 2013 crud operation using web services




function retriveListItem()
{
var soapEnv = “<soapenv:Envelope xmlns:soapenv=’http://schemas.xmlsoap.org/soap/envelope/’> \
<soapenv:Body> \
<GetListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’> \
<listName>companyInfo</listName> \
<viewFields> \
<ViewFields> \
<FieldRef Name=’Company’ /> \
<FieldRef Name=’Industry’ /> \
</ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>”;
$.ajax(
{
url: _spPageContextInfo.webAbsoluteUrl + “/apps/_vti_bin/Lists.asmx”,
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=\”utf-8\””
});
}

function processResult(xData, status)
{
var MainResult = “”;
$(xData.responseXML).find(“z\\:row”).each(function()
{
var companyName = $(this).attr(“ows_Company”);
var Industry = $(this).attr(“ows_Industry”);
MainResult += MainResult + companyName + “-” + Industry + “\n”;
});
$(‘#ResultDiv’).text(MainResult);
}

Create list item using Web Services and jQuery In SharePoint 2013:

sharepoint 2013 create list item web service
sharepoint 2013 create list item web service



Here is the main code in detail:

function createListItem() {
var batch =
“<Batch OnError=\”Continue\”> \
<Method ID=\”1\” Cmd=\”New\”> \
<Field Name=\”Company\”>” + $(“#Company”).val() + “</Field> \
<Field Name=\”Industry\”>” + $(“#Industry”).val() + “</Field> \
</Method> \
ch>”;

var soapEnv =
“<?xml version=\”1.0\” encoding=\”utf-8\”?> \
<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” \
xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” \
xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”> \
<soap:Body> \
<UpdateListItems xmlns=\”http://schemas.microsoft.com/sharepoint/soap/\”> \
<listName>companyInfo</listName> \
<updates> \
” + batch + “</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>”;

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl+ “/apps/_vti_bin/Lists.asmx”,
beforeSend: function(xhr) {
xhr.setRequestHeader(“SOAPAction”,
“http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
},
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=utf-8”
});
}

function processResult(xData, status) {
retriveListItem();
}

Update list item using Web Services and jQuery In SharePoint 2013:

sharepoint 2013 update list item web service
sharepoint 2013 update list item web service

Here is the main code in detail:
function updateListItem() {

var UpdateNewItemXml =
“<Batch OnError=\”Continue\”> \
<Method ID=\”1\” Cmd=\”Update\”> \
<Field Name=\”ID\”>7</Field>\
<Field Name=\”Industry\”>” + $(“#Industry”).val() + “</Field> \
</Method> \</Batch>”;
var soapEnv =
“<?xml version=\”1.0\” encoding=\”utf-8\”?> \
<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” \
xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” \
xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”> \
<soap:Body> \
<UpdateListItems xmlns=\”http://schemas.microsoft.com/sharepoint/soap/\”> \
<listName>companyInfo</listName> \
<updates> \
” + UpdateNewItemXml + “</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>”;

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + “/apps/_vti_bin/Lists.asmx”,
beforeSend: function (xhr) {
xhr.setRequestHeader(“SOAPAction”,
“http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
},
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=utf-8”
});
}
function processResult(xData, status) {
retriveListItem();
}

Delete list item using Web Services and jQuery In SharePoint 2013:

sharepoint 2013 delete list item web service
sharepoint 2013 delete list item web service

here is the main code in detail:
function deleteListItem()
{
var DeleteItemXml = “<Batch OnError=\”Continue\”> \
<Method ID=\”1\” Cmd=\”Delete\”> \
<Field Name=\”ID\”>7</Field>\
<Field Name=\”Company\”>” + $(“#Company”).val() + “</Field> \
</Method> \</Batch>”;
var soapEnv = “<?xml version=\”1.0\” encoding=\”utf-8\”?> \
<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” \
xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” \
xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”> \
<soap:Body> \
<UpdateListItems xmlns=\”http://schemas.microsoft.com/sharepoint/soap/\”> \
<listName>companyInfo</listName> \
<updates> \
” + DeleteItemXml + “</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>”;
$.ajax(
{
url: _spPageContextInfo.webAbsoluteUrl + “/apps/_vti_bin/Lists.asmx”,
beforeSend: function(xhr)
{
xhr.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
},
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=utf-8”
});
}

function processResult(xData, status)
{
retriveListItem();
}

Summary:



In this article we explored SharePoint Web service for (CRUD) operations on list items level. Hope it will be helpful.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →