Sunday, April 17, 2016

SPServices Code to update list items

Hi Folks,

I did an enhancement in my project to update multiple list items using jQuery SPServices. I tried very hard to use EcmaScript to update multiple list items but unfortunately it didn't work out. Finally, I had to depend on my old buddy i.e. SpServices to implement it. Basically, I've a parent list with a Primary Key which acts as a Foreign Key in Child list. From Parent list's Editform.aspx, I've to update the items based on 1 Yes/No field value in parent list which will update the Yes/No field in child list items. The Key is helpful in doing that but we've to use ID field to update items.
Here's the code below.

<script type="../SiteAssets/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../SiteAssets/sputility.min.js"></script>
<script type="text/javascript" src="../SiteAssets/url.js"></script>
<script type="text/javascript" src="../SiteAssets/jquery.SPServices-2013.01.min.js"></script>
<script type="text/javascript" src="../SiteAssets/jquery.SPServices-2013.01.js"></script>


<script type="text/javascript">

var cuidSox;
var context;
var website;
var list;
var items;
var itemCount;
/*$(document).ready(function() {
});*/

function PreSaveAction(){

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', SharePointReady);
// Update SOx list
return true;
}

function SharePointReady(){

cuidSox = $("input[title='Control UID']").val(); //Control UID from Sox control Master List which acts as Primary Key in Parent List and Foreign key in child list

if( $("input[id='ctl00_ctl40_g_48a5562b_7b23_4406_b69b_561200fe8c87_ff151_ctl00_ctl00_BooleanField']").is(':checked')) { 
    var queryXml = "<View><Query><Where><Eq><FieldRef Name = 'ControlUID'/><Value Type='Text'>" + cuidSox + "</Value></Eq></Where></Query></View>";
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
    context = new SP.ClientContext.get_current();
website = context.get_web();

list = website.get_lists().getByTitle('Monitoring List');
items = list.getItems(query);
context.load(items);
context.executeQueryAsync(onRequestSucceeded, onRequestFailed);
else{
//no action needed
}
}

function onRequestSucceeded() {
itemCount = items.get_count();
//alert(itemCount);  
var enumerator = items.getEnumerator();
while (enumerator.moveNext()) {           
var currentItem = enumerator.get_current();
var itemId = currentItem.get_item('ID');
//alert(itemId);
$().SPServices({
  operation: "UpdateListItems",
  async: false,
  batchCmd: "Update",  
  listName: "Monitoring List", 
  ID: itemId, //Without ID field it won't update the values based on parameter value. That's why with Control UID wasn't working before
  valuepairs: [["Retired",1]],
  debug: false,
  completefunc: function(xData, Status) {}
});

}

confirm(itemCount+" items got retired in Monitoring list");
}

function onRequestFailed(sender, args) {
        alert('Error: ' + args.get_message());
}


</script>


Preferred Links:

1 comment:

  1. Casino Online: What is the Best Casino Online in India?
    Casino online https://vannienailor4166blog.blogspot.com/ is one of the top https://access777.com/ types of gambling games to enjoy. If you enjoy https://jancasino.com/review/merit-casino/ casino games, we wooricasinos.info can help you to goyangfc win big.

    ReplyDelete