'sp- How to get the rigth value for Create / Update List
I have a problem that I can't get the right value in the listItemInfo. I tried following to see the output of the code.
alert(listItemInfo.toString());
The first alert is exactly 12 but the second is 1213. I need to check if it is 12 or 13, etc. What is wrong in the script? Here it is. Code:
function retrieveListItems(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('ListA');
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 = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += oListItem.get_item('ID');
//'\nBody: ' + oListItem.get_item('Body');
for (var i = 0; i < listItemInfo.length; i++){
if (listItemInfo.split() == 12) {
var siteUrl = 'https://........'
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('ListA');
var oListItem = oList.getItemById(12);
this.oListItem = oList.getItemById(12);
alert("ist gleich 12");
oListItem.set_item('Location', 'blub');
oListItem.update();
clientContext.executeQueryAsync(onQuerySucceess, onQueryFailed);
}
}
alert(listItemInfo.toString());}
}
function onQuerySucceess() {
alert('Item updated!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Solution 1:[1]
To help you better, can you describe the problem in detail? What function need to be achieved?
In your code, the value of listItemInfo
is a concatenation of all itemIDs. Since alert(listItemInfo.toString());
is placed in the while loop, it will alert itemID values in turn, such as 12, 1213...
By the way, are you trying to change the value of the Item which itemID is 12?
I tried to modify your code for testing and it worked, you can refer to it.
My Test result:
function retrieveListItems() {
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getByTitle('ListTest');
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 = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var listItemInfo = '';
var oListItem = listItemEnumerator.get_current();
listItemInfo += oListItem.get_item('ID');
//'\nBody: ' + oListItem.get_item('Body');
if (listItemInfo == 12) {
var siteUrl = 'https://xxxxx.sharepoint.com/sites/xxxxx'
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('ListTest');
var oListItem = oList.getItemById(12);
this.oListItem = oList.getItemById(12);
alert("ist gleich 12");
oListItem.set_item("Title", "New Title3");
oListItem.update();
clientContext.executeQueryAsync(onQuerySucceess, onQueryFailed);
}
}
}
function onQuerySucceess() {
alert('Item updated!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
------------------------------------update------------------------------------
Please try to put var listItemInfo = '';
into the while loop.then it will alert turn 12 , then 13, then 14 and so on.
Full code:
function retrieveListItems(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Test2');
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 = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var listItemInfo = '';
var oListItem = listItemEnumerator.get_current();
listItemInfo += oListItem.get_item('ID');
//'\nBody: ' + oListItem.get_item('Body');
for (var i = 0; i < listItemInfo.length; i++){
if (listItemInfo.split() == 12) {
var siteUrl = 'https://........'
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('ListA');
var oListItem = oList.getItemById(12);
this.oListItem = oList.getItemById(12);
alert("ist gleich 12");
oListItem.set_item('Location', 'blub');
oListItem.update();
clientContext.executeQueryAsync(onQuerySucceess, onQueryFailed);
}
}
alert(listItemInfo.toString());}
}
function onQuerySucceess() {
alert('Item updated!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |