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>;

Wednesday, April 11, 2012

InfoPath 2010 Repeating Table Updates Field On Change

When working in InfoPath you can run into business situations where a fields value is set based on user selections in other fields.  This can add a layer of complexity when the fields exist in a Repeating Table as each row needs to be executed independently.  But do nt worry there is a simple solution for this requirement as each InfoPath form is just a structured XML document on the back end.  This solution is given as code behind as this provides the quickest solution.  The sample below executes on a change event of a control in a repeating table and not on the insertion of a row as this would present null issues in the code.

public void Control_Changed(object sender, XmlEventArgs e)

{
//check to ensure value changed and not just row add if so run the code
if (e.Operation == XmlOperation.ValueChange)
{
//connect to current row in dom
XPathNavigator xnDoc = e.Site.CreateNavigator();
xnDoc.MoveToParent();                               
//select node that was changed in current row
string myVar = xnDoc.SelectSingleNode("my:Node", this.NamespaceManager).Value.ToString();
//select node in current row to update
XPathNavigator UpdateNode = xnDoc.SelectSingleNode("my:NodeToUpdate"this.NamespaceManager);
UpdateNode.SetValue(myVar);
}

}