Some Advanced InfoPath Rules
In this tutorial I will create a “smart” InfoPath form that will change its contents based on some scenarios.
Scenario
- employees register each month to certain company benefits for the upcoming month.
- subscriptions are open from 24th until the end of the month. outside of this period, the subscriptions are closed.
- once an employee has registered, they cannot register again in that month (we can show instead a thank you page).
Solution
This can be achieved by creating a Site Workflow that registers the data into a SharePoint List. The Workflow form would be customized with InfoPath. Below I will provide details about the form.
Prerequisites
1. Have a SharePoint List for storing the registrations.
Required Fields:
Person
(data type: Person)MonthTag
(data type: String). A unique string to identify the year/month for which the registration applies to; eg:2012-11
. Later this will also allow easy grouping and filtering.RegistrationID
(data type: String). Combination of the person’s Username and MonthTag, to determine if a person already subscribed for that month. The value is calculated by the workflow, and updated at creation time. eg:2012-11.Levente.Rog
)- Any other fields that need input from the user.
2. A Site Workflow that creates the registration
We create a workflow adds an entry into the list. Users will have to Start the workflow (we’ll be providing them with a link).
The Workflow will have the following steps:
- Generate the
MonthTag
(eg:2012-11
) - Generate the
RegistrationID
(eg:2012-11.Levente.Rog
) - Create an Item in the Subscriptions List with the previously mentioned values. Using the
Workflow Context:Initiator
variable we already know the identity of the user - Send confirmation email
Upon request, I can create a tutorial explaining in greater detail covering these workflow steps.
3. Form Customization
In this step we’ll customize the Workflow Form.
This is how I managed to solve this scenario:
Here I created three separate sections, for each of the scenarios (disregard the 201-12. section for now, I will explain that later).
1. Registrations are open
- First scenario is when registrations are open. Two conditions have to fulfill for this section to be displayed:
- Date of month >= 24
- User has not subscribed yet in the upcoming month
To achieve this, I created the following InfoPath Formatting rules with the action to Hide the section.
- The expression:
number(substring(xdDate:Today(), 9, 2)) < 24
- Any occurence of
RegistrationID
is equalto concat(substring(addDays(today(), 30), 1, 7), ".", translate(userName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))
What I did with that function is:
Generate a “MonthTag” that is unique for each month. Since registrations happen for upcoming month, we calculate one month in advance (addDays 30
) and we only keep the first 7 characters from the ISO Date, to get something like 2012-11
. We concatenate this with the lowercase username.
This way we’ll get something like:
2012-11.levente.rog
. We also record that same string in the SharePoint list as well. This way we know if that user has already registered for that month.
Any occurrence of RegistrationID - we get that by creating a second connection to the same SharePoint List (Data Ribbon -> To SharePoint List). If any of those empty comes out true, the section will be hidden.
2. Thank you page
If the user has already registerd, we’ll show a thank you message.
This rule is basically a negation of rule #2
- All occurences of RegisrationID that are not equal to
concat(substring(addDays(today(), 30), 1, 7), ".", translate(userName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))
3. Registrations are closed
Show a registrations are closed message if date is <= 24th.
This rule is the negative of Rule #1.
- The expression:
number(substring(xdDate:Today(), 9, 2)) \>= 24
4. Dummy section
The “2012-12” section is just a calculated value containing the RegistrationID generation formula.
For some reason, when I haven’t actually used this formula, it was not evaluated in the rule. This section is always hidden from the end user (by creating a dummy rule: myfield does not equal “asdf” => Hide).
- Calculated Value XPath expression:
concat(substring(xdDate:AddDays(xdDate:Today(), 30), 1, 7), ".", translate(xdUser:get-UserName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))
That’s it. Save and Publish.
The workflow can be redirected to itself by changing the &Source=
URL parameter to the same workflow link. For example:
http://sptest/Subscriptions/\_layouts/IniWrkflIP.aspx?TemplateID={fd570692-3e3e-4c6d-a613-d11932bfc5e2}&Source=http://sptest/Subscriptions/\_layouts/IniWrkflIP.aspx?TemplateID={fd570692-3e3e-4c6d-a613-d11932bfc5e2}
After completing the workflow, it will be redirected to itself again, showing the same form. But since the user now is subscribed, the “Thank you” section will show up this time.
Originally posted to: http://www.thesysadminhimself.com/2012/11/some-advanced-infopath-rules.html
Comments