I am having a bug in one of the visual web parts. First of all, this is not my code and I am not an expert in javascript.
Secondly, this issue is only happening the first time after the pool has been recycled or the web application has been restarted, the rest of the times is working fine.
Explanation: the web part has a repeating section created using javascript, when the form is submitted, there are 2 events:
OnClick="btnAdvanceSubmit_Click" OnClientClick="SaveItems()"
SaveItems() is saving the repeating sections using clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
btnAdvanceSubmit_Click is starting a workflow and closing the form. if I include Thread.Sleep(3500) here the process finish properly and everything is saved fine. if I don't, the repeating section is not saved. This issue happens ONLY after an iisreset or similar operation.
Please, find my code below: javascript
function SaveItems()
{
var table = document.getElementById('tableAdvanceItems');
var rowLength = table.rows.length;
var formID = <%=expenseFormID%>;
for (var i = 1; i < rowLength; i++) {
var row = table.rows[i];
var cellLength = row.cells.length;
var id = row.cells[0].children[0].value;
var exchangeRateSelect = row.cells[5].children[0];
var advanceType = $(row.cells[1]).find("input[type='radio']:checked").val();
var advanceBank = $(row.cells[2]).find("input[type='radio']:checked").val();
var advanceAccount = row.cells[3].children[0].value;
var amount = row.cells[4].children[0].value;
var exchangeRate = exchangeRateSelect.options[exchangeRateSelect.selectedIndex].value;
var currency = exchangeRateSelect.options[exchangeRateSelect.selectedIndex].text;
var amountLC = row.cells[6].children[0].value;
if (id) {
UpdateAdvanceItem(id, advanceType, advanceBank, advanceAccount, amount, exchangeRate, amountLC, formID, currency); }
else {
InsertNewAdvanceItem(advanceType, advanceBank, advanceAccount, amount, exchangeRate, amountLC, formID, currency); }
}
}
function InsertNewAdvanceItem(advanceType, advanceBank, advanceAccount, amount, exchangeRate, amountLC, formID, currency) {
var siteUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
var clientContext = new SP.ClientContext(siteUrl);
var list = clientContext.get_web()
.get_lists()
.getByTitle('Advance Items');
this.listItem = list.addItem(itemCreateInfo);
listItem.set_item('AGLAdvanceType', advanceType);
listItem.set_item('AGLAdvanceTransferBank', advanceBank);
if (advanceAccount) {listItem.set_item('AGLAdvanceTransferBankAccount', advanceAccount); }
if (amount) {listItem.set_item('AGLAdvanceItemAmount', amount); }
listItem.set_item('AGLExpenseItemExchangeRate', exchangeRate);
listItem.set_item('AGLAdvanceCurrency', currency);
if (amountLC) {listItem.set_item('AGLAdvanceItemAmountLocalCur', amountLC); }
listItem.set_item('AGLExpenseFormID', formID);
var itemCreateInfo = new SP.ListItemCreationInformation();
listItem.update();
clientContext.load(listItem);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
C#
protected void btnSave_Click(object sender, EventArgs e){using (SPLongOperation longOperation = new SPLongOperation(this.Page)) { longOperation.LeadingHTML = "Saving Data";
longOperation.TrailingHTML = "Please wait while the data is being saved.";
// Thread.Sleep(3500);
longOperation.Begin();
item = item ?? expensesList.AddItem();
SaveData();
longOperation.EndScript("window.frameElement.commitPopup();");
StartWorkflow("Advance-Expense Form Reminder Workflow");
}
string close = @"<script type='text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onQuerySucceeded() {}
Is there any way to avoid this issue? if I include the commented line "Thread.sleep" will work, but I don't want the user to wait 3.5 seconds to submit a form.
I have tried do a SharePoint warm up using PowerShell, but still the same issue.
Thanks for your help.
Bets regards,
Eriel
clientContext.executeQueryAsync
is committing all the stuff that was done toclientContext
before that, but you didn't show any of that. And what doonQuerySucceeded
andonQueryFailed
do? In general, something is probably timing out because of the cold start (i.e. IIS reset), but that's about all I know at the moment.