Home > Work > Useful JavaScript Function: PreSaveAction #SP2010 #SharePoint #PS2010 #ProjectServer #in

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:

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:

1 <script type='text/javascript'> 2 3 //global variable to be used in multiple functions 4 var currentPathname = location.pathname; 5 //set to lowercase to make comparisons in the code easier 6 currentPathname = currentPathname.toLowerCase(); 7 8 function PreSaveAction() {; 9 10 if (currentPathname.indexOf('Enter Pathname here IE. /lists/tasks/editform.aspx') != -1) { 11 //Setup your validation checks here 12 if (1 == 1) { 13 alert('Validation passed, let SharePoint continue'); 14 return true; 15 } 16 else { 17 alert('Validation failed, stop SharePoint from saving the item'); 18 return false; 19 } 20 } 21 22 //If we have got to this point, then no pages need pre-validation. Let SharePoint continue. 23 return true; // OK to proceed with the save item 24 } 25 26 </script>

About these ads
  1. March 11, 2012 at 13:00

    Great resources

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,679 other followers

%d bloggers like this: