SharePoint2013 Use of PreSaveAction Function on list forms

sharepoint 2013 presave action.png
Check out Best Alternative to InfoPath


In this Article we explore how to use PreSaveAction() method in SharePoint list form in SharePoint 2013. PreSaveAction() a JavaScript function which helps us to do something before the item will be saved.


PreSaveAction function allows to override functionality when Save button is clicked. PreSaveAction function executes the code written for validation on click of save button. I just want to add one thing, you should override PreSaveItem – this is a SharePoint built in function. Instead, like mentioned above – override/create PreSaveAction.

Read some SharePoint 2013 tutorials below:

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



Solution:
I used below code with PreSaveAction function for validation to display messages on 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 save button is clicked and messages are displayed in case of File not attached.

sharepoint 2013 presave action.png
sharepoint 2013 presave action.png

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


You May Also like the Following SharePoint Online Tutorials:

About 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.

View all posts by Sagar Pardeshi →