SharePoint2013: How to use of PreSaveAction() on list forms

This SharePoint tutorial, we will check, how to use PreSaveAction() method in the SharePoint list form in SharePoint 2013. PreSaveAction() a JavaScript function that helps us to do something before the item will be saved.

PreSaveAction() in SharePoint

PreSaveAction function allows overriding functionality when the Save button is clicked. PreSaveAction() executes the code written for validation on the click of the save button.

I just want to add one thing, you should override PreSaveItem – this is a SharePoint built-in function. Instead, as mentioned above – override/create PreSaveAction.

I have a default SharePoint list form with a save or cancel button, I want the user to attach files while creating a new item. When the user clicks on the save button, the page postback happens and if validation fails, validation message is displayed.

I used the below code with PreSaveAction function for validation to display messages on the Save button click and Save button, attach runtime custom handler. Default Save button calls PreSaveItem method, which in turn calls PreSaveAction for any validations.

$(document).ready(function () {
ProducerReferral();
attachEventHandlers();// function for checking the duplication of files
$(‘input[value=Save]’).click(function () {
PreSaveTest ();
});
});

function PreSaveTest()
{
PreSaveAction();
}

function PreSaveAction() {
if($("#idAttachmentsRow").css("display")=="none"){
$("#part1 > h4")[1].innerHTML += "<span style=’margin-left: 40px;’ class=’ms-formvalidation ms-csrformvalidation’>Please Attach Files.</span>";
returnVal = false;
}
else { return true ;}
}

This method is called when the save button is clicked and messages are displayed in the case of File not attached.

sharepoint 2013 presaveaction

Note: If you are using “$(document).ready” in your code, make sure the PreSaveAction function is written outside this block, as in the above code or else the PreSaveAction method will not be called.

You may like following SharePoint tutorials:

Here, we learned how to use PreSaveAction() in SharePoint list forms.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

>