Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

SharePoint 2010 InfoPath Forms and Rules

5.00/5 (1 vote)
17 Jun 2012CPOL4 min read 43.9K  
Exploring InfoPath Forms with Rules.

Introduction

In the previous article we have seen the basics of InfoPath forms and the integration with SharePoint 2010. In this article we can explore InfoPath Forms with Rules.

What are Rules?

A rule in InfoPath is an Action which takes place on an Event. An event could be a field value change. There could be multiple Actions for an Event.

Types of Rules

Please note that there are three types of Rules:

  1. Validation Rule
  2. Formatting Rule
  3. Actions

Creating the Form

Open InfoPath Designer 2010 and create a new List from SharePoint List template. Select your website and enter the name of list as My Rule List.

Image 1

1. Validation Rule

Validation rule validates a field value and displays an error message. We can use range comparison, email validations etc. using this rule.

We are trying to add a Validation rule for:

  • Email field which will Validate itself and shows a message if Invalid email entered

Use the Add field link from the left pane to create a new field named Email. Once the field is added select the field and from the ribbon menu click the Add Rule button. Then from the menu select the option Is Not an Email Address > ShowValidation Error.

Image 2

After selecting the item, you can see that the Rule pane comes into the play.  You can use the Rule Pane to manage the rules assigned.

Image 3

Now Drag and Drop the Email field to the form.  Save and Publish it. 

Inside the SharePoint site try to enter an email which is invalid and you should get an error as shown below:

Image 4

2. Formatting Rule

Formatting rule are associated with controls rather than fields.  In case if we need to format the color of a textbox based on a condition this rule is the apt one.

In this scenario we are adding a Field named Age.  If the Age is greater than 100, the textbox should turn red in color.  Let us do it.

Remove the previous field from the Form and add a new field named Age. Set the type of field to Number as shown below:

Image 5

Now drag the field to our form.

Image 6

Once the form is ready, select the Age Textbox and from the ribbon menu choose menu item Add Rule > Is Greater than > Bad

Image 7

You will get the following dialog to capture the Max value.

Image 8

Enter 100 as the value and click the OK button. 

Good! Now you are read with the Formatting Rule.  Save & Publish the form and inside SharePoint try to add a value greater than 100.

Image 9

You can see that the color changed to red. 

3. Actions

Now we can try with the Action.  Action allow to perform the following:

  • Query Data from other Source
  • Set another Field Value
  • Submit the Form
  • Get Data from a Web Part

Please note that there could be more than one action associated with a  field.

Our scenario is to have 2 fields:

  1. Country of type text
  2. Currency of type text

When the Country name is India, the Currency had to be set to Rupee.

When the Country name is America, the Currency had to be set to Dollar.

Let us see how to make this work:

Now you can remove the previous fields, and add the above two fields.

Image 10

Now click on the Country field and from the ribbon choose Manage Rules. In the appearing Rule pane click the New Action button.

A new Rule will be created.  Now click on the Condition link:

Image 11

A new dialog will be appearing.  Enter the detail as shown below:

Image 12

So this adds our first condition, when the Country field is equal to India.  Now we have to set the Action for it.

Click on the Add button from the Rules pane and choose Set a field’s value. In the appearing dialog, choose the field Currency and set the value to Rupee.

Image 13

Now you have added the condition and action for Rule 1.

Similarly you can add one more rule, check for Country is America and set the Currency field to Dollar.

Now Save & Publish the form.  Inside SharePoint try entering India and press the tab button.  You can see that the Currency field is set to Rupee.

Image 14

So this concludes our Actions rule.

From the Rules pane, you can delete / disable a rule based on the requirement. The changes are reflected in just a Save and Publish.

References

Summary

In this article we have explored InfoPath forms with Rules enabled. In a real life scenario, this is one common feature used by people.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)