Friday, March 24, 2017

SPServices:How to access List Data using SPServices jQuery library in SharePoint

How to access List Data using SPServices jQuery library in SharePoint

In my previous article, what is SPServices, I already explained about this JavaScript add-on.
Now, I am going to explain how to access List Data using SPServices. SPServices JavaScript Library ($().SPServices) provides function called GetListItems to access the data from list.
The GetListItems function details are:
[pre class="brush:jscript" ] $().SPServices({ operation: "GetListItems", async: {true or false}, listName: {list name}, CAMLQuery: {caml query}, CAMLQueryOptions: {caml Query Options}, webURL: {weburl} }); [/pre]
  • operation: name of the $().SPServices operation is "GetListItems"
  •  async: you can pass true or false
  •  listName: Name of the list from which data needs to be fetched
  •  CAMLQuery: You can mention the CAML query XML with filters. For example, if one of the column name in list is IsAction, then sample CAML query would be:
  • [pre class="brush:jscript" ] var camlQuery = ''; camlQuery += ''; camlQuery += 'Yes'; camlQuery += ''; camlQuery += ''; [/pre]
  • CAMLQueryOptions: You can mention CAML query options like Recursive. For example, you have to fetch the data from sub-folder also then sample CAML query options would be:
  • [pre class="brush:jscript" ] var camlQueryOptions = ''; [/pre]
  •  webURL: This allows you to change the context for the operation to a different site

Now, complete $().SPServices.GetListItems operation call would be:
[pre class="brush:jscript" ] function getListDetails() { var objResults = null; var camlQueryOptions = ''; var camlQuery = ""; var listName = 'EmployeeDetails'; //--To get all Active items camlQuery = ""; camlQuery += ""; camlQuery += "Yes"; camlQuery += ""; camlQuery += ""; var listPromise = $().SPServices({ operation: "GetListItems", async: false, listName: listName, CAMLQuery: camlQuery, CAMLQueryOptions: camlQueryOptions }); listPromise.success(function () { if ($(listPromise.responseText).find("ErrorText").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "servicecallError" }'); console.log($(listPromise.responseXML).find("ErrorText").text()); console.log(objResults); } else if ($(listPromise.responseXML).find("row").length > 0 || $(listPromise.responseXML).SPFilterNode("z:row").length > 0) { //--If success var objListItem; var listData = []; var tempDate = null; var newDate = null; $(listPromise.responseXML).SPFilterNode("z:row").each(function () { objListItem = new Object(); if (!commonfunctions.helpers.isNullOrEmpty($(this).attr('ows_ID'))) { objListItem.itemId = $(this).attr('ows_ID'); } else { objListItem.itemId = "0"; } if (!commonfunctions.helpers.isNullOrEmpty($(this).attr('ows_Title'))) { objListItem.empEnterpriseId = $(this).attr('ows_Title'); } else { objListItem.empEnterpriseId = ""; } if (!commonfunctions.helpers.isNullOrEmpty($(this).attr('ows_EmpName'))) { objListItem.empName = $(this).attr('ows_EmpName'); } else { objListItem.empName = ""; } if (!commonfunctions.helpers.isNullOrEmpty($(this).attr('ows_EmpEmail'))) { objListItem.empEmail = $(this).attr('ows_EmpEmail'); } else { objListItem.empEmail = ""; } if (!commonfunctions.helpers.isNullOrEmpty($(this).attr('ows_IsAccepted'))) { objListItem.isAccepted = $(this).attr('ows_IsAccepted'); } else { objListItem.isAccepted = ""; } listData.push(objListItem); objListItem = null; }); objResults = ({ status: success, message: listData }); console.log(objResults); } else { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "servicecallError::Status Text:In succcess->else block." }'); console.log(objResults); } }); listPromise.error(function () { if ($(listPromise.responseXML).find("errorstring").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "' + $(listPromise.responseXML).find("errorstring").text() + '" }'); console.log(objResults); } }); listPromise.fail(function () { if ($(listPromise.responseXML).find("errorstring").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "' + $(listPromise.responseXML).find("errorstring").text() + '::Status Text:' + listPromise.statusText + '" }'); console.log(objResults); } }); return objResults; } [/pre]


With the help of above function, you can get the data from the list using SPServices JavaScript library.

More articles on SPServices:
What is SPServices
How to access List Data using SPServices jQuery library in SharePoint
How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint
How to create/add folder in SharePoint List or Document Library using SPServices jQuery library in SharePoint
Update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint

Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels