Jul 12
Making Date Offsets work in InfoPath
Tim Allison
After graduating in 1989 I worked as a consultant, then off to Herman Miller as a Network Admin, and finally landed at C/D/H. So ultimately 21 years! Wow!
I enjoy making life better: at home, at play and at work. Technology happens to be one of those areas. In the same manner helping those around me become better through sharing knowledge only results in a better place for us all.
Family is important to me. I am finding my granddaughter (yes, I have three grandchildren) is always on the top of my “love to spend time with” list.
More about Tim
Articles by Tim Allison
Background
A client needed an InfoPath form that allowed the form-filler to enter an employment plan start date.
After 6 months there was a re-evaluation. They wanted the 6-month expiration date to be displayed several places in the form.
This was simple to start. Insert a date picker in the form. But how about the expiration date? It needed to be an exact 6-month offset (e.g. Feb 1 -> Aug 1).
The client tried to use an InfoPath rule on the date picker field. This included the built-in Add_Days (field, number of days) function. However they were using a 180-day offset, to simulate 6 months, because there wasn’t an InfoPath rule function for Add-Months().
It seemed plausible, but it didn’t work. It ended up producing inconsistent dates because the number of days in each month varied.
What I did
I used code-behind. By adding a Programming -> Changed Event to the date picker I was able to do an exact 6-month offset. I got the date picker value as a DateTime value and used the DateTime.AddMonths() function.
The Code
public
void PlanStartDate_Changed(object sender, XmlEventArgs e)
{
// When the date changes it is first deleted. We do not want to process it when this happens. (other Operations are Insert)
if
(e.Operation.ToString() == “Delete”)
return
;
// Get the date picker data source
XPathNavigator
xPSD = this.CreateNavigator().SelectSingleNode(”/my:myFields/my:PlanStartDate”, NamespaceManager);
// Thankfully IP has the ValueAsDateTime making the translation to DateTime easy. (DateTime is required as you will see)
DateTime
dtPSD = xPSD.ValueAsDateTime;
// Here is where the real work happens. Just use the AddMonths() function!
dtPSD = dtPSD.AddMonths(6);
// Set the remaining form dates.
// Get access to the other fields that will use the newly offset date.
XPathNavigator
xDate1 = this.CreateNavigator().SelectSingleNode(“/my:myFields/my:Date1″, NamespaceManager);
// This is precautionary. If this has not been set with a default value, or set before the XML contains a NIL, you will get a schema validation error. I recommend you always do this when setting an IP field in code.
DeleteNil(xDate1);
// Now the value. InfoPath requires the date to be entered in the yyyy-MM-ddThh:mm:ss format.
// I am skipping the time as it is not required for this
xDate1.SetValue(dtPSD.ToString(
“yyyy-MM-dd”));
}
Summary
Sometimes InfoPath rules just don’t cut it. Don’t be afraid to step into the code-behind. It will make your life much easier.
NOTE: You must have Visual Studio Tools for Applications (VSTA) installed as a part of your InfoPath installation.



