PART 1 – Coding in InfoPath

Hi! I am back with an article on InfoPath again. When someone says, you are an InfoPath expert, I say with sad face “No, I do not know about InfoPath Code behind and Digital Signatures”. No more worries on InfoPath Code behind. Thanks to my lead who introduced me to the world of InfoPath code behind.

Before going through the steps for code behind , make a note that coding can be done only in (InfoPath)Form library and not in List forms. Let us see how to create code behind file in InfoPath.


  • Click on File tab -> Form Options(Advanced form options). In Form Options dialog box, click on Programming on left and choose the form template code language as shown below


  •  Go to Developer tab, click on Language and select the language/Change Language

  • Click on Code Editor(located next to Language in Developer tab) to see the FormCode.cs file(code behind file)
  • To write code for a button click, right click on the button ->Button Properties…->Edit form Code

Microsoft Visual Studio Tools for Applications opens as shown below

  • To validate a field or to set some action when the field’s value is changed, use the Validating event/Changed event in Programming  in fields pane or in Control Events section in developer tab

Control Events

So far, we have seen how to add code in InfoPath. Now, let us see the basic syntax/code

InfoPath is a xml file where all the fields are saved as xml nodes. To select or navigate to the nodes, XPathNavigator is used.

To get a field value in InfoPath, syntax goes as:

XPathNavigator xpn = this.MainDataSource.CreateNavigator();
string fn = xpn.SelectSingleNode(“XPATH OF THE FIELD“, this.NamespaceManager).Value;

To set a field value in InfoPath, syntax is

XPathNavigator xpn = this.MainDataSource.CreateNavigator();
XPathNavigator temp = xpn.SelectSingleNode(“XPATH OF THE FIELD, this.NamespaceManager); temp.SetValue(“Hello Sathiya”);

If you notice the above syntax, the first argument of SelectSingleNode is xpath of a field. To find the xpath of a field:

  • Go to Fields pane, click on down arrow dropdown of a field and select Copy Xpath as depicted below

To query or submit data connection, syntax is

DataConnection submitConn = DataConnections[“DATA CONNECTION NAME“];

To switch among the views, syntax is:

this.ViewInfos.SwitchView(“Thank you”);


With these basics, we will try to do magic 😛 in InfoPath form in the upcoming article 🙂


This file cannot be saved because some properties are missing or invalid


Editing a word document in SharePoint and saving it, throws the below error

Doc Properties Message

The error clearly states that required or mandatory column is not filled. When we check the properties by clicking “Go to Document Properties”, we see the core and mandatory column, Author as blank but they were filled already. Though we fill and save it back in word document properties, it becomes blank.



Before uploading this document in sharepoint, author has inspected the document and removed the document properties and personal information as shown below



Click on File->Options to see Word Options dialog box. In the dialog box, select Trust center on left and click Trust center Settings button located under Microsoft Word Trust Center

Click on Privacy options and uncheck “Remove Personal information from file properties on save as shown below

Hit OK button and try to save the document.  Yayy! The error is  vanished.

InfoPath Preview to the rescue

In this post, let’s see about Preview option in InfoPath. It is not always recommended or necessary to publish the changes in order to verify it. To verify the changes made in InfoPath, use the preview option.

Preview for New forms:

Let us assume you are designing a new form and you want to verify then and there. Instead of publishing the changes every time, use the below steps:

  • Click on Home tab in InfoPath ribbon (if the current tab is not Home)
  • Click on Preview in Form section as shown below

Preview for new forms

Preview for Existing forms:

What if you are making an enhancement to the exiting form and would like to know how it behaves for already submitted form. Please follow the below steps to verify the behavior:

  • Go to the form library, select the submitted form and click on Download a copy in the ribbon as shown below

Download a copy

  • Go to InfoPath Design form and click on File->Form Options(Advanced Form Options)

Form Options

  • In Form options, click on Preview, browse the downloaded form under Sample data and click ok

Preview for Existing forms

  • Click on Home tab->Preview in Form section to see the changes in submitted form
  • If you have assigned User roles to the InfoPath form, you can change the user role to preview the submitted form as depicted below

Preview using different user roles

Error: Access Denied in approving an item


There is a InfoPath form library with unique permission set for each item(form) using “Replace permission” action in SharePoint Designer Workflow. When an item is created, an email is sent to the Approver with Contribute permission set by the workflow. If approver opens it, it shows access denied message.


Check if InfoPath form library has the below library configurations in Versioning Settings:

  • Require content approval for submitted items – Yes
  • Who should see draft items in this document library – Only users who can approve items (and the author of the item)


Here are the possible solutions you can implement based on your scenario.

  1. If Content approval is not required, change it to “No” to avoid the above scenario
  2. If content approval is required, then approver can approve bulk items to allow others to see and edit the item
  3. If you do not wish to make any change in content approval, then you can change the Draft item Security(Who can see Draft Items) to Only users who can edit items
  4. If you do not wish to change the Content approval as well as Draft Item security, then you must change the permission assigned by the workflow to provide Approve access. This will work only for the items that will be created thereafter but for existing items, permission should be granted manually or using script


No item exists. It may have been deleted or removed by another user

In this article, let us see one of the scenario for which the error is shown as “No item exists at {item url}. It may have been deleted or removed by another user”


You have a SharePoint list customized in InfoPath for course registration where whenever user registers for a course, an email with item url is sent to the approver for approval. Once the approver clicks on the link in email, the below message is shown.

No Item exists error



  • Go to List settings->Advanced settings
  • Check if item-level permissions is set as “Read items that were created by the user” for Read access and “create items and edit items that were created by the user” for create and edit access
  • This setup in item level permission indicates that only users can see and edit the items created by them


  • Item level permission applies to users with Contribute or Edit access. So, to see and edit other’s item(override item level permission), the approvers should be given Design or Full Control Permission.
  • Another solution is to reset the item level permission configurations to default and use other ways of restricting access to items such as Replace permission using SharePoint designer workflow, SharePoint view filter for CreatedBy is equal to [Me]


Repeating Table – set color for alternative rows and auto number the rows

Set color for alternative rows:

Logic behind : Odd number of rows(1,3,5,7..) when divided by 2 will return the remainder 1  and even number of rows(2,4,6,8..) when divided by 2 will return the remainder 0. To find the remainder, we use mod(modulo) operator. To find whether the row is odd number or even number, we can use position() in infopath.

  • Go to Home->Manage rules
  • In fields pane, select the group containing repeating fields and add formatting rule
  • Under condition, click on None and select “The expression” in first drop down
  • To color the odd rows, enter the expression as position() mod 2 > 0 or position() mod 2 = 1
  • Under Formatting, select the color that you would like to apply in Shading(bucket symbol)

Color alternative rows

  • Click on Home->Preview to see the result

Auto number repeating table rows:

  • To auto generate number in SNO inside repeating table, right click on SNO ->Text Box Properties
  • Under Default values, click on fx and insert the formula, count(../preceding-sibling::my:group2) + 1 and click ok

  • You can also use position() instead of the above formula but this will not be supported in InfoPath 2013 text box. However, it can be used in Calculated value in InfoPath 2013. Right click the SNO ->Change Control->Calculated Value and enter position() in XPath

  • Click on Home -> Preview to see the result.


Repeating Table – Set default number of rows and limit the total number of rows

Repeating table plays a vital role in form library to insert several rows of data in a single form. In this post, let’s see two different methods of setting the default number of rows and limiting the total number of rows in repeating table.

METHOD 1 – Using Default Values and Rules

Set default number of rows:

  • Go to Data tab in the ribbon and click on Default Values
  • In Edit Default values dialog box, right click on group that contains the repeating table fields and select “Add another group below”. Repeat this step to increase the default number of rows
  • Click on Home->Preview to see the result.

  • Limitation – You will not be able to remove the rows inserted by this method

Limit total number of rows:

  • Go to Home tab and click on Manage rules
  • Select the group(group2 in above picture) that contains repeating fields in Fields pane
  • In Rules pane, click on New -> Formatting
  • Under condition, select “The expression” in first drop down, type the expression value as count(/my:myFields/my:group1/my:group2) = 6
  • Under Formatting, tick the box “Don’t allow users to insert or delete this control” as depicted below

  • Now, when the user inserts the 6th row and click on insert item, rows will not be created and drop down highlighted below shown till 5th row will disappear on inserting 6th row to prevent insertion and deletion.

METHOD 2: (Modifying the Source File)

  • After designing the form with repeating table, click on Publish -> Export Source Files

  • Browse the location and type the folder name to save the source file

  • Close the InfoPath form and open “myschema.xsd” from source file folder

  • To set default number of rows, change the minOccurs value as shown below
  • To limit the total number of rows, change the maxOccurs value as shown below

  • Save myschema.xsd and close it
  • Open manifest file in Design mode, preview and publish it
  • Now, when the user tries to insert 11th row, below error will be shown.