Monday, April 16, 2012

SharePoint 2010 PreSaveAction() saves the day...

Looking for a quick way to do some validation when working on a SharePoint 2010 Foundation product and you don't have access to the InfoPath toolbox that SharePoint Server 2010 provides??  Let me introduce you to PreSaveAction() javascript call!  This function is available in 2007 and 2010 it is just a little easier to access in the 2010 platform with the ability to edit list forms through the ribbon.  Below is a quick script from MSDN for validating that attachments are added to a List Item in a Custom List but this is just the beginning.  You could verify column formatting and any other PreSubmit work that you need to happen on your custom lists.  Place the script in a ContentEditor webpart on your NewForm.aspx and EditForm.aspx pages and you are ready to Validate your buns off!!

   <script language="javascript" type="text/javascript">;

function PreSaveAction() {
var attachment = document.getElementById("idAttachmentsTable");

if (attachment == null || attachment.rows.length == 0) {

document.getElementById(
"idAttachmentsRow").style.display = 'none';

alert("Attachments are required on this list.");
return false;
}else { return true; }

} </script>;

No comments: