Useful JavaScript Function: PreSaveAction #SP2010 #SharePoint #PS2010 #ProjectServer #in
Just a quick blog post to talk about a standard JavaScript function that exists in SharePoint 2007 and 2010 – PreSaveAction().
Now I have done extra validation and functions on list forms in allsorts of ways before (some of which is blogged on this site) and it is only recently that this standard function has been brought to my attention. (Thanks @GlynClough via twitter).
Once I learned about this function I of course google’d about it to see what I could find and came across the following blog articles:
- http://www.endusersharepoint.com/2008/10/07/endusersharepointcom-extending-issues-and-tasks-part-3/
- http://www.allaboutmoss.com/index.php/tag/presaveaction/
These are excellent articles that explain the function, however in the way I tend to work in my projects it posed a problem.
We are essentially overriding a standard SharePoint JavaScript function and the articles assume that you can place a Content Editor Web Part onto the page to add the functionality.
Now in SharePoint 2010 this is less of a problem, since there are very good and official ways to add web parts to list form pages, even in a Publishing Site.
However, in Microsoft Office SharePoint Server 2007 utilising publishing sites, editing newform.aspx and editform.aspx pages caused many issues.
This forced the consultant / developer to create their own new / edit forms and pages in order to insert sometimes a very small amount of code.
Since most of my projects seem to involve the Publishing feature in one way or another, I typically place JavaScript reference files in the Master Page. This means that my JavaScript code is running on every page.
So, getting to the point of this article, I have modified the general example that is supplied on other sites to take into account this method of JavaScript development by simply querying what page I am on before I run my validation tests and functions.
Code Example:
Great resources