1,804 articles and 14,819 comments as of Sunday, May 8th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Tuesday, June 29, 2010

SharePoint: Extending the DVWP – Part 14: Putting PreSaveAction() to Work with jQuery

Author: Jim Bob Howard

Now that we know how to press PreSaveAction() into service on the DVWP and have employed jQuery to create some variables, let’s write our variables out to an audit list.

New Audit Records Using SharePoint Web Services

Marc’s library offers a quick way to record the changes to our audit trail, using UpdateListItems.

First, write the old (or From) record…

    $().SPServices({
        operation: "UpdateListItems", // The Cmd below causes this to be an insert
        async: false,
        listName: "FTE Change Audit", // The 'Display Name' of audit list I've created
        updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                "<Method ID='1' Cmd='New'>" +
                    "<Field Name='FromTo'>From</Field>" + // Field Name is the 'Static Name' of the list column
                    "<Field Name='Title'>" + txtOldName + "</Field>" +
                    "<Field Name='Location'>" + txtOldLoc + "</Field>" +
                    "<Field Name='Group'>" + txtOldGrp + "</Field>" +
                    "<Field Name='Position'>" + txtOldPos + "</Field>" +
                    "<Field Name='WorkShift'>" + txtOldShift + "</Field>" +
                    "<Field Name='FTE'>" + txtOldFTE + "</Field>" +
                    "<Field Name='EffDate'>" + txtEffDate + "</Field>" +
                    "<Field Name='ChangeType'>" + txtChangeType + "</Field>" +
                "</Method>" +
            "</Batch>",
        completefunc: function(xData, Status) {
        }
    });

…then repeat the process for the new (or To) record. Change the FromTo value to To; notice we’re using the same EffDate for both of them (your requirements may vary). ChangeType can be set automatically or calculated based on what has changed.

All Together Now

Edit as you have need, but here’s how my PreSaveAction() looks:

  function PreSaveAction() {
     var txtChangeType = "Update";

     var txtNewName = $("input[name*=$ff1_]").val();
     var txtOldName = $("span[id*=_ff9_]").text();

     var txtNewPos = $("input[name*=$ff3_]").val();
     var txtOldPos = $("span[id*=_ff11_]").text();

     var txtNewShift = $("select[name*=$ff4_]").val();
     var txtOldShift = $("span[id*=_ff12_]").text();

     var txtNewFTE = $("input[name*=$ff5_]").val();
     var txtOldFTE = $("span[id*=_ff13_]").text();

     var txtNewGrp = $("input[name*=$ff2_]").val();
     var txtOldGrp = $("span[id*=_ff10_]").text();

     var txtNewLoc = $("input[name*=$ff6_]").val();
     var txtOldLoc = $("span[id*=_ff14_]").text();

     var txtEffDate = $("input[name*=$ff7_]").val();

     $().SPServices({
          operation: "UpdateListItems",
          async: false,
          listName: "FTE Change Audit",
          updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                    "<Method ID='1' Cmd='New'>" +
                         "<Field Name='FromTo'>From</Field>" +
                         "<Field Name='Title'>" + txtOldName + "</Field>" +
                         "<Field Name='Location'>" + txtOldLoc + "</Field>" +
                         "<Field Name='Group'>" + txtOldGrp + "</Field>" +
                         "<Field Name='Position'>" + txtOldPos + "</Field>" +
                         "<Field Name='WorkShift'>" + txtOldShift + "</Field>" +
                         "<Field Name='FTE'>" + txtOldFTE + "</Field>" +
                         "<Field Name='EffDate'>" + txtEffDate + "</Field>" +
                         "<Field Name='ChangeType'>" + txtChangeType + "</Field>" +
                    "</Method>" +
               "</Batch>",
          completefunc: function(xData, Status) {
          }
     });

     $().SPServices({
          operation: "UpdateListItems",
          async: false,
          listName: "FTE Change Audit",
          updates: "<Batch OnError='Continue' PreCalc='TRUE'>" +
                    "<Method ID='1' Cmd='New'>" +
                         "<Field Name='FromTo'>To</Field>" +
                         "<Field Name='Title'>" + txtNewName + "</Field>" +
                         "<Field Name='Location'>" + txtNewLoc + "</Field>" +
                         "<Field Name='Group'>" + txtNewGrp + "</Field>" +
                         "<Field Name='Position'>" + txtNewPos + "</Field>" +
                         "<Field Name='WorkShift'>" + txtNewShift + "</Field>" +
                         "<Field Name='FTE'>" + txtNewFTE + "</Field>" +
                         "<Field Name='EffDate'>" + txtEffDate + "</Field>" +
                         "<Field Name='ChangeType'>" + txtChangeType + "</Field>" +
                    "</Method>" +
               "</Batch>",
          completefunc: function(xData, Status) {
          }
     });
     return true;
};
  

Other Uses

What can you do with the PreSaveAction()?

  1. Field validation – making sure the data you’re about to write to the database is going to make sense, e.g.
    1. End date comes after Start date
    2. A date range is at least X days (or no more than X days)
    3. A person is already (or not yet) on a list
    4. If some optional fields become required based on other data filled in
    5. ad infinitum
  2. Add "Did you mean…?" functionality
    1. "…to make a change, but not update the Effective date?"
    2. "…to set this date in the future?"
    3. "…this person whose name is similar?"
    4. "…for this to be a transfer? or a termination?"
    5. ad infinitum
  3. Write to an audit trail
  4. Confirm delete
  5. Look up information to store in this item
  6. YOUR idea…

Want more?

Shoban Kumar, Wael Abbas, and Edin Kapic, along with EUSP authors Michael Greene and Marc Anderson (individually, and in conversation) offer some other ideas for using the PreSaveAction().

Next time: What do user-managed drop-down lists have to do with the DVWP? Well, they’re just good practice whether you’re using the DVWP or not. But, if you want to use cascading dropdowns on your DVWP, you’ll need to plan ahead. We’ll start walking through that plan and point out pitfalls along the way.

Four articles from now, you’ll have cascading dropdowns on your DVWP. And your list owner will manage the content and the relationships from SharePoint lists.

Author: Jim Bob Howard

Jim Bob Howard is a web designer / webmaster in the healthcare industry. He has been working with SharePoint since March 2009 and enjoys sharing what he has learned. He is a moderator and frequent contributor to Stump the Panel, and answers SharePoint questions on Twitter (@jbhoward) and via email ([email protected]).

View all entries in this series: Extending the DVWP»
Entries in this series:
  1. SharePoint: Extending the DVWP - Part 1: Layout Enhancement - Rearranging Columns - Default and Edit Templates
  2. SharePoint: Extending the DVWP - Part 2: Layout Enhancement - Rearranging Columns - Insert Template
  3. SharePoint: Extending the DVWP – Part 3: Getting it All on One Line - DVWP Function Action Links
  4. SharePoint: Extending the DVWP – Part 4: Turning DVWP Action Links into Buttons
  5. SharePoint: Extending the DVWP – Part 5: Doing Stuff Before Save on Submit - PreSaveAction()
  6. SharePoint: Extending the DVWP – Part 6: Examining the Form Action Links
  7. SharePoint: Extending the DVWP – Part 7: Creating a Form Action Workflow
  8. SharePoint: Extending the DVWP – Part 8: Creating a Form Action Workflow - The After Math
  9. SharePoint: Extending the DVWP – Part 9: Oops! Failed Setting Processor Stylesheet
  10. SharePoint: Extending the DVWP – Part 10: Passing Workflow Variables to a Form Action Workflow
  11. SharePoint: Extending the DVWP – Part 11: Getting More Form Fields to the Workflow
  12. SharePoint: Extending the DVWP – Part 12: Adding More Form Fields from the Data
  13. SharePoint: Extending the DVWP – Part 13: Putting PreSaveAction() to Work – Creating Variables
  14. SharePoint: Extending the DVWP – Part 14: Putting PreSaveAction() to Work with jQuery
  15. SharePoint: Extending the DVWP – Part 15: User-Managed Dropdowns with Site Columns
  16. SharePoint: Extending the DVWP – Part 16: User-Managed Dropdowns - Loading Data
  17. SharePoint: Extending the DVWP – Part 17: User-Managed Dropdowns – Creating a Relationship list
  18. SharePoint: Extending the DVWP – Part 18: User-Managed Dropdowns – Loading the Relationship list – Part 1
  19. SharePoint: Extending the DVWP – Part 19: User-Managed Dropdowns – Loading the Relationship list – Part 2
  20. SharePoint: Extending the DVWP – Part 20: Cascading Dropdowns - Applying the jQuery
  21. SharePoint: Extending the DVWP – Part 21: Cascading Dropdowns - Three-tier Cascade
  22. SharePoint: Extending the DVWP – Part 22: Creating Title Based on Other Fields with jQuery
  23. SharePoint: Extending the DVWP – Part 23: Creating Title Based on Other Fields with a Workflow
  24. SharePoint: Extending the DVWP – Part 24: A Note to Readers
  25. SharePoint: Extending the DVWP – Part 25: Using an Audit Trail by Creating List Items with SPServices
  26. SharePoint: Extending the DVWP – Part 26: Modifying the Edit Template
  27. SharePoint: Extending the DVWP – Part 27: Adding an Alternate Edit Template to a DVWP
  28. ExSharePoint: Extending the DVWP – Part 28: Massage the Remove Template
  29. SharePoint: Extending the DVWP – Part 29: Modifying Form Action Workflows on the remove Template
  30. SharePoint: Extending the DVWP – Part 30: Using EasyTabs with Filtered DVWPs to Make Data Manageable
  31. SharePoint: Extending the DVWP – Part 31: Filling in Default Data on the insert Template with jQuery
  32. SharePoint: Extending the DVWP – Part 32: Filling in Default Data on the insert Template with Multiple DVWPs
  33. SharePoint: Extending the DVWP – Part 33: Modifying Total and Subtotal Row Layouts in DVWP
  34. SharePoint: Extending the DVWP – Part 34: Using Icons for Form Action Links
  35. SharePoint: Extending the DVWP – Part 35: Putting it All Together
  36. SharePoint: Extending the DVWP – Bonus: Fixing the Insert Form Action When "No Matching Items"
  37. SharePoint: Extending the DVWP – Bonus: Creating a Title Based on Dropdowns with jQuery
 

Please Join the Discussion

4 Responses to “SharePoint: Extending the DVWP – Part 14: Putting PreSaveAction() to Work with jQuery”
  1. How else have you used the PreSaveAction() ?

Trackbacks

Check out what others are saying about this post...
  1. [...] in Part 14 of this series, "Putting PreSaveAction() to Work with jQuery," I gave a quick snapshot of how you might [...]

  2. [...] form variables, and then add a few more. And we’ll add more variables that we can use with jQuery and the PreSaveAction() for form validation or writing to an audit [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!