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()?
- Field validation – making sure the data you’re about to write to the database is going to make sense, e.g.
- End date comes after Start date
- A date range is at least X days (or no more than X days)
- A person is already (or not yet) on a list
- If some optional fields become required based on other data filled in
- ad infinitum
- Add "Did you mean…?" functionality
- "…to make a change, but not update the Effective date?"
- "…to set this date in the future?"
- "…this person whose name is similar?"
- "…for this to be a transfer? or a termination?"
- ad infinitum
- Write to an audit trail
- Confirm delete
- Look up information to store in this item
- 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]).
- SharePoint: Extending the DVWP - Part 1: Layout Enhancement - Rearranging Columns - Default and Edit Templates
- SharePoint: Extending the DVWP - Part 2: Layout Enhancement - Rearranging Columns - Insert Template
- SharePoint: Extending the DVWP – Part 3: Getting it All on One Line - DVWP Function Action Links
- SharePoint: Extending the DVWP – Part 4: Turning DVWP Action Links into Buttons
- SharePoint: Extending the DVWP – Part 5: Doing Stuff Before Save on Submit - PreSaveAction()
- SharePoint: Extending the DVWP – Part 6: Examining the Form Action Links
- SharePoint: Extending the DVWP – Part 7: Creating a Form Action Workflow
- SharePoint: Extending the DVWP – Part 8: Creating a Form Action Workflow - The After Math
- SharePoint: Extending the DVWP – Part 9: Oops! Failed Setting Processor Stylesheet
- SharePoint: Extending the DVWP – Part 10: Passing Workflow Variables to a Form Action Workflow
- SharePoint: Extending the DVWP – Part 11: Getting More Form Fields to the Workflow
- SharePoint: Extending the DVWP – Part 12: Adding More Form Fields from the Data
- SharePoint: Extending the DVWP – Part 13: Putting PreSaveAction() to Work – Creating Variables
- SharePoint: Extending the DVWP – Part 14: Putting PreSaveAction() to Work with jQuery
- SharePoint: Extending the DVWP – Part 15: User-Managed Dropdowns with Site Columns
- SharePoint: Extending the DVWP – Part 16: User-Managed Dropdowns - Loading Data
- SharePoint: Extending the DVWP – Part 17: User-Managed Dropdowns – Creating a Relationship list
- SharePoint: Extending the DVWP – Part 18: User-Managed Dropdowns – Loading the Relationship list – Part 1
- SharePoint: Extending the DVWP – Part 19: User-Managed Dropdowns – Loading the Relationship list – Part 2
- SharePoint: Extending the DVWP – Part 20: Cascading Dropdowns - Applying the jQuery
- SharePoint: Extending the DVWP – Part 21: Cascading Dropdowns - Three-tier Cascade
- SharePoint: Extending the DVWP – Part 22: Creating Title Based on Other Fields with jQuery
- SharePoint: Extending the DVWP – Part 23: Creating Title Based on Other Fields with a Workflow
- SharePoint: Extending the DVWP – Part 24: A Note to Readers
- SharePoint: Extending the DVWP – Part 25: Using an Audit Trail by Creating List Items with SPServices
- SharePoint: Extending the DVWP – Part 26: Modifying the Edit Template
- SharePoint: Extending the DVWP – Part 27: Adding an Alternate Edit Template to a DVWP
- ExSharePoint: Extending the DVWP – Part 28: Massage the Remove Template
- SharePoint: Extending the DVWP – Part 29: Modifying Form Action Workflows on the remove Template
- SharePoint: Extending the DVWP – Part 30: Using EasyTabs with Filtered DVWPs to Make Data Manageable
- SharePoint: Extending the DVWP – Part 31: Filling in Default Data on the insert Template with jQuery
- SharePoint: Extending the DVWP – Part 32: Filling in Default Data on the insert Template with Multiple DVWPs
- SharePoint: Extending the DVWP – Part 33: Modifying Total and Subtotal Row Layouts in DVWP
- SharePoint: Extending the DVWP – Part 34: Using Icons for Form Action Links
- SharePoint: Extending the DVWP – Part 35: Putting it All Together
- SharePoint: Extending the DVWP – Bonus: Fixing the Insert Form Action When "No Matching Items"
- SharePoint: Extending the DVWP – Bonus: Creating a Title Based on Dropdowns with jQuery
How else have you used the PreSaveAction() ?