Sharepoint not saving attachment on EditForm.aspx
-
16-01-2021 - |
문제
I'm customizing the default EditForm.aspx of a list in Sharepoint with Javascript directly on a Script Editor content webpart on the page.
One item that I have customized is the Save Button event.
Fisrt I remove the default event of the Button:
$("[name*='SaveItem']:visible").removeAttr("onclick");
After I use my custom code to save form
$("[name*='SaveItem']:visible").on("click", function(e){
$("#loading").show();
var answer = confirm("Save Item?");
if (answer)
{
if (!PreSaveItem()){
return false;
}
if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')){
return false;
}
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($("[name*='SaveItem']:visible").attr("id"), "", true, "", document.location.origin, false, true));
}
else{
$("#loading").hide();
}
});
When I try to attach a item, everything seems correct. The file appears in the final of the form.
But when I click to Save, all fields are updated, but the attachment file is not saved in the item.
Note: If I follow the same steps but click on default Save Button on the sharepoint ribbon, the attachmente is saved on item.
Any suggestions to try to solve this problem?
해결책
There are two issues with you initial code in the line where you call WebForm_DoPostBackWithOptions
:
WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(
$("[name*='SaveItem']:visible").attr("id"), // eventTarget
"", // eventArgument
true, // validation
"", // validationGroup
document.location.origin, // actionUrl
false, // trackFocus
true)); // clientSubmit
Looking at the code of the original onclick
event of the save button, it expects different values for the first (eventTarget) and fifth (actionUrl) parameter:
- Instead of the
id
of the button, the first parameter should actually be thename
attribute of the button (theid
is basically thename
with all$
replaced by_
) - Instead of
document.location.origin
, an empty string (""
) is expected as the actionUrl
So the changed line should look like this:
WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(
$("[name*='SaveItem']:visible").attr("name"), // eventTarget
"", // eventArgument
true, // validation
"", // validationGroup
"", // actionUrl
false, // trackFocus
true)); // clientSubmit
The answer by Anders Aune is actually not correct, as window.form
refers to the same object as SPClientForms.ClientFormManager.GetClientForm("WPQ2")
. So those two lines actually do the same thing:
SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')
window.form.SubmitClientForm()
다른 팁
Have you tried submitting the form with the form variable, that should work:
$("[name*='SaveItem']:visible").on("click", function(e){
$("#loading").show();
var answer = confirm("Save Item?");
if (answer)
{
if (!PreSaveItem()){
return false;
}
if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')){
return false;
}
window.form.SubmitClientForm();
}
else{
$("#loading").hide();
}
});
Change the dopostback with window.form.SubmitClientForm();