Sunday, April 02, 2017

SPServices:Update multiple items in SharePoint List or Document Library using SPServices jQuery library in SharePoint

Update multiple items in SharePoint List or Document Library using SPServices jQuery library in SharePoint

In SharePoint 2013 we can utilize client side code to update the list item. The client side scripting includes:

  • JSOM (JavaScript Object Model)
  • REST API using JQuery AJAX call
  • SPServices JavaScript Add-on


In this article, i will explain How to use SPUpdateMultipleListItems function of SPServices JavaScript add-on to Update or Delete Multiple/Single Items.

The details of $().SPServices. SPUpdateMultipleListItems operation are:

[pre class="brush:jscript" ] $().SPServices({ webURL: {web URL}, async: {true/false}, listName: {List Name}, CAMLQuery: {caml Query}, batchCmd: {"Delete" or "Update"}, valuepairs: {key/value pair of columns}, ID: {Id of the row, when update record}, completefunc: function (xData, Status) { console.log('List items has been updated'); } }); [/pre]

  • webURL: This allows you to change the context for the operation to a different site
  • async: you can pass true or false
  • listName: Name of the list from which data needs to be fetched
  • CAMLQuery: The CAMLQuery option allows you to specify the filter on the list. CAMLQuery here should contain valid CAML such as:

         [pre class="brush:jscript" ] var camlQuery = ''; camlQuery += ''; camlQuery += "Yes; camlQuery += ''; camlQuery += ''; [/pre]
  • batchCmd: We can set value as "Delete" or "Update".
  • valuepairs: It is an optional parameter and used to define Key/Value pair. If you specify updates then valuepairs should not be specified.


valuepairs: [["Title", "Amit Kumar"], ["Technologies", ".NET, SharePoint, AngularJS"]]

  •     ID: It’s an optional parameter and needs to be used with valuepairs  when we are using the batchCmd as "Update".


Update single item the SharePoint List using $().SPServices. SPUpdateMultipleListItems operation with valuepairs option:
[pre class="brush:jscript" ] var camlQuery = ''; camlQuery += ''; camlQuery += "Yes; camlQuery += ''; camlQuery += ''; $().SPServices({ async: false, batchCmd: "Update", listName: "AmitKumar_TestList", ID: 1, valuepairs: [["Title","Amit Kumar"]], CAMLQuery: camlQuery, completefunc: function (xData, Status) { console.log('List items has been updated'); } }); [/pre]




In the above code block, we are using "Update" batchCmd with ID attribute to update single item.

Delete mulitple item the SharePoint List using $().SPServices. SPUpdateMultipleListItems operation with valuepairs option: 

[pre class="brush:jscript" ] var camlQuery = ''; camlQuery += ''; camlQuery += "Yes; camlQuery += ''; camlQuery += ''; $().SPServices({ async: false, batchCmd: "Delete", listName: "AmitKumar_TestList", CAMLQuery: camlQuery, completefunc: function (xData, Status) { console.log('List items has been updated'); } }); [/pre]

In the above code block, we are using "Delete" batchCmd with CAMLQuery attribute to delete multiple items.

The SPUpdateMultipleListItems function works like this:

It first calls GetListItems with the provided CAMLQuery to find all off the items which meet the criteria
Then the function calls UpdateListItems and updates all of the items found with the values provided.

Reference: https://github.com/sympmarc/SPServices/wiki/SPUpdateMultipleListItems   

Share:

Wednesday, March 29, 2017

SPServices:Delete item in SharePoint List or Document Library using SPServices jQuery library in SharePoint


Delete item in SharePoint List or Document Library using SPServices jQuery library in SharePoint

In my previous article, Update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint, I already explained about $().SPServices.UpdateListItems operation to access the listing of folder.

For example, in SharePoint online site, we need to delete item in SharePoint List or Document library on the basis of key field when user click on button.

To implement above requirement, we can not use the SharePoint Server Side code, so we need to utilize the Client Side Scripting – JSOM/Call to SharePoint REST API using JQuery/Use of SPServices JQuery Library.

Now, I am going to explain how to delete item in SharePoint List or Document Library using SPServices jQuery library in SharePoint. SPServices JavaScript Library ($().SPServices) provides function called UpdateListItems to delete content in the SharePoint list.

Delete item the SharePoint Document Library or List using $().SPServices.UpdateListItems operation with batchCmd option:

[pre class="brush:jscript" ] $().SPServices({ operation: "UpdateListItems", async: false, listName: "AmitKumar_TestList", batchCmd: "Delete", ID: 3, completefunc: function(xData, Status) { console.log('List items has been deleted'); } }); [/pre]

In the above code block, we are not using updates attributes of "UpdateListItems" operation, instead using batchCmd attribute to delete row on the basis of ID.


Delete item in SharePoint List using $().SPServices.UpdateListItems operation with CAML query:

[pre class="brush:jscript" ] var id = 1; var camlQuery = '' + '' + '' + id + '' + '' + ''; var objPromise = $().SPServices({ operation: "UpdateListItems", async: false, listName: "AmitKumar_TestList", updates: camlQuery }); p.done({ // Manage success or failure here }); [/pre]

In the above code block, we are using updates attributes of "UpdateListItems" operation to delete item in SharePoint list.


Delete file in SharePoint Document Library using $().SPServices.UpdateListItems operation with CAML query:

[pre class="brush:jscript" ] var id = 1; var fileRef = "http://amitkumarmca04.blogspot.com/AmitLib/amitkumar.doc"; var camlQuery = '' + '' + '' + id + '' + '' + fileRef + '' + '' + ''; var objPromise = $().SPServices({ operation: "UpdateListItems", async: false, listName: "AmitKumar_TestList", updates: camlQuery }); p.done({ // Manage success or failure here }); [/pre]

In the above code block, we are using extra field called "FieldRef" with "ID" field to delete document from SharePoint document library.
In both cases, we are passing an update, which contains a batch. We can request multiple actions in the batch.

Delete multiple items in SharePoint list using $().SPServices.UpdateListItems operation with CAML query:

[pre class="brush:jscript" ] //--Sample of creating array of rows--::Start::------------// var objArrColumns = []; var objArrRows = []; //---First Row Details :: Start::------------// var objColumnDetails = new Object(); //--ID column details objColumnDetails.FieldName = "ID"; objColumnDetails.FieldValue = 1; objArrColumns.push(objColumnDetails); //--Add details to column array objArrRows.push(objArrColumns)//--Add details to Row array //---First Row Details :: End::------------// //---Second Row Details :: Start::------------// objArrColumns = null; objArrColumns = []; //--ID column details objColumnDetails = null; objColumnDetails = new Object(); objColumnDetails.FieldName = "ID"; objColumnDetails.FieldValue = 2; objArrColumns.push(objColumnDetails); //--Add details to column array objArrRows.push(objArrColumns)//--Add details to Row array //---Second Row Details :: End::------------// //--Sample of creating array of rows--::Start::------------// [/pre]


[pre class="brush:jscript" ] //-- Output would be //camlQuery = '12'; //--In simple way /* var camlQuery ='' + '' + '1'+ '' + '' + '2'+ '' + ''; */ function DeleteMultipleItems (){ var objPromise = $().SPServices({ operation: "UpdateListItems", async: false, listName: "AmitKumar_TestList", updates: camlQuery , }); return objPromise; } [/pre]

In the above code block, we are using batch command to perform delete action on multiple rows.


Share:

Tuesday, March 28, 2017

SPServices:Update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint

In my previous article, How to create folder inside SharePoint List or Document Library using SPServices jQuery library in SharePoint, I already explained about $().SPServices.UpdateListItems operation to access the listing of folder.

For example, in SharePoint online site, we need to update item in SharePoint List or Document library on the basis of key field when user click on button.

To implement above requirement, we can not use the SharePoint Server Side code, so we need to utilize the Client Side Scripting – JSOM/Call to SharePoint REST API using JQuery/Use of SPServices JQuery Library.

Now, I am going to explain how to update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint. SPServices JavaScript Library ($().SPServices) provides function called UpdateListItems to update content in the SharePoint list.
  • Update item the SharePoint Document Library or List using $().SPServices.UpdateListItems operation with valuepairs option:
       [pre class="brush:jscript" ] $().SPServices({ operation: "UpdateListItems", async: false, batchCmd: "Update", listName: "AmitKumar_TestList", ID: 1, valuepairs: [["Title","Amit Kumar"]], completefunc: function (xData, Status) { console.log('List items has been updated'); } }); [/pre]
     In the above code block, we are not using updates attributes of "UpdateListItems" operation,
    instead using valuepairs attribute to define the columns which needs to be updated on the basis of ID of element/row.
   We can define more than one columns in valuepairs attribute, it will serve as an array of columns:
valuepairs: [["Title", "Amit Kumar"], ["Technologies", ".NET, SharePoint, AngularJS"]]
  • Update item in SharePoint Document Library or List using $().SPServices.UpdateListItems operation with CAML query:
[pre class="brush:jscript" ] var camlQuery = '' + '' + 'Amit Kumar' + '.NET, SharePoint, AngularJS' + '1' + '' + ''; $().SPServices({ operation: "UpdateListItems", async: false, listName: "AmitKumar_TestList", updates: camlQuery , completefunc: function(xData, Status) { console.log('List items has been updated'); } }); [/pre]

In the above code block, we are not using batchCmd/ID attributes of "UpdateListItems" operation, instead using updates attribute to define the columns which needs to be updated based on ID of element/row. The default for the batchCmd option is 'Update'


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:

SPServices:How to create/add folder in SharePoint List or Document Library using SPServices jQuery library in SharePoint

In my previous article, How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint, I already explained about $().SPServices.GetListItems operation to access the listing of folder.

Take an example of one requirement: In SharePoint online site, we need to create the folder in SharePoint List or Document library when user click on button.

To implement above requirement, we can not use the SharePoint Server Side code, so we need to utilize the Client Side Scripting – JSOM/Call to SharePoint REST API using JQuery/Use of SPServices JQuery Library.

Now, I am going to explain how to create/add folder in SharePoint List or Document Library using SPServices jQuery library in SharePoint. SPServices JavaScript Library ($().SPServices) provides function called UpdateListItems to update content in the SharePoint list.
The $().SPServices.UpdateListItems function details are:

[pre class="brush:jscript" ] $().SPServices({ operation: "UpdateListItems", async: {true/false}, listName: {List Name}, batchCmd: {"New" or "Update"}, updates: {caml Query}, valuepairs: {key/value pair of columns}, ID: {Id of the row, when update record}, webURL: {web URL} }); [/pre]


  • operation: name of the $().SPServices operation is "UpdateListItems "
  •  async: you can pass true or false
  •  listName: Name of the list from which data needs to be fetched
  •  batchCmd: We can set value as "New" or "Update".
  •  updates: It is an optional parameter and used to define caml Query. If you specify valuepairs then updates should not be specified.
  • valuepairs: It is an optional parameter and used to define Key/Value pair. If you specify updates then valuepairs should not be specified.
  • ID: It’s an optional parameter and needs to be used with valuepairs  when we are using the batchCmd as "Update".
  • webURL: This allows you to change the context for the operation to a different site

Note that if you decide to use the valuepairs approach, you also need to specify the ID option. The default for the batchCmd option is 'Update'. Also, the choices for Cmd are [New, Update, Delete, Moderate].

Now, Create folder inside the SharePoint Document Library or List using $().SPServices.UpdateListItems operation with CAML query:
[pre class="brush:jscript" ] var folderName= 'Amit_Kumar'; var camlQuery = '' + '' + '1' + '' + folderName + '' + '' + folderName + '' + '' + ''; $().SPServices({ operation: "UpdateListItems", async: false, listName: "documents", updates: camlQuery , completefunc: function(xData, Status) { //--You can perform any action, when create request completed } }); [/pre]

In the above code block, we are not using batchCmd/valuepairs/ID attributes of "UpdateListItems" operation, because CAML query contains information about command type and all required details.
Share:

Sunday, March 26, 2017

SPServices:How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint

In my previous article, How to access List Data using SPServices jQuery library in SharePoint, I already explained about $().SPServices.GetListItems.

Now, I am going to explain how to get listing of folders including sub-folders in a SharePoint List or Library with 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  to get listing of folders including the sub-folders would be:

[pre class="brush:jscript" ] function geFolderListing() { var folderPromise = null; var objResults = null; var listName = 'EmployeeDetails'; folderPromise = $().SPServices({ operation: "GetListItems", async: false, listName: listName, }); folderPromise.success(function () { if ($(folderPromise.responseText).find("ErrorText").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "servicecallError" }'); console.log($(folderPromise.responseXML).find("ErrorText").text()); console.log(objResults); } else if ($(folderPromise.responseXML).find("row").length > 0 || $(folderPromise.responseXML).SPFilterNode("z:row").length > 0) { //--If success var objListItem; var listData = []; $(folderPromise.responseXML).SPFilterNode("z:row").each(function () { objListItem = new Object(); objListItem.folder = $(this).attr('ows_Title'); 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); } }); folderPromise.error(function () { if ($(folderPromise.responseXML).find("errorstring").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "' + $(folderPromise.responseXML).find("errorstring").text() + '" }'); console.log(objResults); } }); folderPromise.fail(function () { if ($(folderPromise.responseXML).find("errorstring").length > 0) { //--If error occured objResults = JSON.parse('{ "status": "error", "message": "' + $(folderPromise.responseXML).find("errorstring").text() + '::Status Text:' + folderPromise.statusText + '" }'); console.log(objResults); } }); return objResults; } [/pre]
With the help of above function, you can get the listing of all folders including sub-folders from the SharePoint list or document library 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:

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