I am trying to update list item's value through JavaScript object model in SP 2013. When I call set_item('fieldname','mvalue')
, it does not throw any error,
but it fails to update. How to successfully update a ListItem
?
I am able to get the current item id, and after set_item()
is getting called, my alert('testing for update...')
is also working, but it seems that some problem exists in clientContext.executeQueryAsync()
or load()
event.here is my code:
$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(GetDesc, "sp.js");
});
function GetDesc()
{
context = new SP.ClientContext.get_current();
web = context.get_web();
siteURL=$(location).attr('href');
var titleName=document.title;
var mListName= (titleName.substring(0,titleName.lastIndexOf("-"))).trim();
mList = web.get_lists().getByTitle(String(mListName));
context.load(mList);
alert(titleName+"!");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where></Query>
<RowLimit>10</RowLimit></View>');
this.collListItem = surveyList.getItems(camlQuery);
context.load(collListItem);
context.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
alert("Else");
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
alert("while");
var curritem = listItemEnumerator.get_current();
alert(curritem.get_id());
curritem.set_item("Testing","Test");
alert("Updated");
curritem.update();
alert("Updated1");
//clientContext.executeQueryAsync(onQuerySucceeded1, onQueryFailed1);
//clientContext.load(curritem);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
alert("Updated2");
//clientContext.executeQuery();
}
}
function onQuerySucceeded(sender, args) {
var desc = surveyList.get_description();
desc=desc.trim();
if(desc.lastIndexOf("Closed") != -1)
{
alert("Quiz has been closed");
//window.location.assign("/sites/Test/SitePages/Home.aspx");
//window.location.assign(window.parent.location.href);
//window.parent.location.Reload();
//window.location.Reload();
window.location.assign(siteURL.substring(0,
siteURL.lastIndexOf("Lists/")));
} else {
alert("Updated");
}