Hi Sharepoint experts,
I'm trying to add list item using jsom and succeeded but my code keeps adding empty list item first.
function repeat(pattern, count) {
if (count < 1) return '';
var result = '';
while (count > 1) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
return result + pattern;
}
function onCountSucceeded() {
varNoPR = '';
varListCnt = oList.get_itemCount();
if (varListCnt === 0) {
varListCnt = 1;
}
else {
varListCnt += 1;
}
varTmpStr = varListCnt.toString();
varStrLen = varTmpStr.length;
varNoPR = repeat('0', 10 - varStrLen).concat(varTmpStr);
}
function getLastNumber() {
var clientContext2 = new SP.ClientContext(varsiteUrl);
this.oList = clientContext2.get_web().get_lists().getByTitle(listName);
clientContext2.load(this.oList);
clientContext2.executeQueryAsync(
Function.createDelegate(this, this.onCountSucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function createItem() {
var clientContext = new SP.ClientContext(varsiteUrl);
var oList = clientContext.get_web().get_lists().getByTitle(listName);
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', 'My New Item!');
oListItem.set_item('NoPR', varNoPR);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onAddItemSucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function createListItem() {
getLastNumber();
createItem();
}
So my goal is inserting custom auto increment number by counting the list items first and then add '0' so the resulting number becomes '0000000001', ...etc. But when I run this twice (I assigned this code to button), it resulted in empty item, and then item 0000000001. What I am doing wrong here ?
Thanks for any help...