'Create and Update a List Item with Javascript in Sharepoint
Hello i'm an beginner and i try to create/update a SP List with Javascript with an condition. if a list entry with current users email exist then i will only update the field location otherwise i will add a new entry. could you please help me
function createUpdateListItem() {
var siteUrl = 'https://........';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var currentuserID = currentUser.get_email();
var e = document.getElementById("choice");
var stdort = e.options[e.selectedIndex].text;
var user = oList.get_item('Email');
if (user === currentuserID) {
var listItem = list.getItem('Email');
listItem.set_item('Location', stdort);
listItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceededUpdate), Function.createDelegate(this, this.onQueryFailedUpdate)
);
}
function onQuerySucceededUpdate() { alert('Item updated successfully !');
}
function onQueryFailedUpdate(sender, args) { alert('Could not able to update item: ' + args.get_message());
}
}
if user != currentuserID {
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Employee', currentUser.get_title());
oListItem.set_item('Email', currentUser.get_email());
oListItem.set_item('Location', stdort);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceededCreate),
Function.createDelegate(this, this.onQueryFailedCreate)
);
}
function onQuerySucceededCreate() { alert('Item created: ' + oListItem.get_id());
}
function onQueryFailedCreate(sender, args) { alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
}
if i create separate functions as example only create then it works, but i need create or update a list entrie
Solution 1:[1]
First, please check that the {} in your code is correctly paired, such as "if user != currentuserID" is not in the function , then, please write the condition of the if statement in ().
Here is an example of creating and updating a list that you can refer to:
function retrieveListItems(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('List Name');
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, function(){
var listItemEnumerator = collListItem.getEnumerator();
var listItemInfo="";
while (listItemEnumerator.moveNext()) {
var olistitem = listItemEnumerator.get_current();
listItemInfo +=olistitem.get_item('Title');
if(listItemInfo =="fff"){
this.oListItem = oList.getItemById(olistitem.get_id());
oListItem.set_item('Title', 'ccc');
oListItem.update();
clientContext.executeQueryAsync(onsuccessUpdate, onQueryFailed);
}
else if(listItemInfo !="fff") {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', 'fff');
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(onsuccessCreate, onQueryFailed);
}
break;
}
})
); } function onsuccessCreate() {alert('Item created: ' + oListItem.get_id());} function onsuccessUpdate() {alert('Item updated!');} function onQueryFailed(sender, args) {alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}
Solution 2:[2]
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 =
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('List1');
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 | Zella_msft |
Solution 2 |