Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Calling code/script from a Rule

Calling code/script from a Rule

  • Comments 7

Rules are a quick way to add dynamic functionality to a form, so a lot of programmers choose to use rules even when they know how to write the code to do the same thing. However, sometimes after writing a complex set of rules they may realize that they need to do something rules doesn’t support, and we know they don’t want to rewrite all their rules in code just to add that one little bit.

Alas, there’s no rules action type for “Call code”, but there is a roundabout way to call your code anyway.

Let’s say you have the following function in your form template:

Function Test(param)
  XDocument.UI.Alert("Param: " & param)
  Test = true
End Function
function Test(param) {
  XDocument.UI.Alert("Param: " + param);
  return true;
Public Function Test(ByVal param As String) As Boolean
  thisXDocument.UI.Alert("Param: " & param)
  Test = True
End Function
public boolean Test(string param) {
  thisXDocument.UI.Alert("Param: " + param);
  return true;


You can call that function from the condition of a rule by using the xdExtension namespace.

For example, here’s how to call the function from a rule when the form is opened:

  1. Click Form Options on the Tools menu.
  2. On the Open and Save tab, click Rules.
  3. Click Add, name the rule, and then click Set Condition.
  4. Select The expression in the first drop-down list, and then enter: xdExtension:Test(“foo”)
  5. If you want the function to be conditional, then put the condition before the function call with an AND clause. For example, to get if foo>bar, then Test(“foo”), use foo>bar and xdExtension:Test(“foo”). (This is possible because conditions are evaluated using short-circuit boolean evaluation. In other words, they are evaluated one clause at a time, and if the first clause determines the result, then the rest of the conditions are “short circuited” and don’t get executed.)
  6. The Rule dialog box requires at least one action, but if you don’t have any actions to run in that rule, you can click OK if you select the Stop processing rules when this rule finishes checkbox. To avoid actually stop processing rules, make your condition always return false. For example: xdExtension:Test(“foo”) and false (“false” here is actually looking for the node named “false” which doesn’t exist, so returns the empty set, which is translated to false(). You could just as easily use “foobar” or anything else.)

Finally, extract your form files and add the following attribute to the manifest.xsf file’s xDocumentClass element:


Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
  • IP2007 - I though this could be a work around for an issue, but I'm having a failure within my code.

    Is it possible to have a DataConnection.Execute() in the function call when used in this context?



  • Does IP Forms Service Support this construct?

    Also, IP2007 seems to have the xdExtension pre defined.

  • On MOSS form services upload/Verify:

    The upload process returns the following Validation error:


    The following expression could not be parsed because of a syntax error

    or because it uses an undefined namespace prefix or unsupported

    function: xdExtension:of_PreSave("foo").

    Location:  manifest.xsf

  • Is this supported in IP2010?

  • yes, this is still supported

  • i learning for office infopath..... but until now i can understand...,, cause i write some questions on here

  • When I try to add the xdExtension line to the manifest.xsl file, I get an error about multiple definitions (that namespace is already defined in the manifest.xsl file).  When I try to build a rule using xdExtension:Test("Foo"), I get the error "Reference to undeclared namespace prefix: 'xdExtension'."

    These errors together don't make sense--I cannot add the namespace because it is already present in the file, but when I try to use the namespace I get an error about it not being defined.

Page 1 of 1 (7 items)