This is the second post from ‘No-Coding Solutions’ series. The first post showed you how to open any InfoPath form on a custom SharePoint web part page. In this post will show you how to customize display form for a list, and more specific – how to customize display value for a field.
The specific task I had is to show ‘No (click edit to modify)’ or ‘Yes (click edit to modify)’ as display value for a Boolean field when item is in read-only mode. As you know, Boolean fields are usually displayed as a checkbox in InfoPath forms. Of course, you can modify the control type to drop-down, radio buttons, text box, and other. But the type of control you select will be used across all views of the SharePoint form – new, edit and display. In my case I needed to have a checkbox for edit and new, and a simple text for display.
Create new InfoPath View
First I created additional view into the InfoPath form (‘Display Mode’) and set it to be read only from view’s Properties section.
After that did a copy of the entire content from ‘Edit Item’ to ‘Display Mode’ (just use copy-paste-all). Once this was done deleted the checkbox from display view and placed a calculated value control. For XPath data source selected the Boolean field and published the form back to SharePoint.
Associate View form
When display view was ready, I opened the list, selected ‘List’ tab, selected ‘Form Web Parts’ and clicked on ‘(Item) Display Form’ option.
This opened display form in edit mode. Then opened ‘InfoPath Form Web Part’ web part in edit mode, and selected ‘Display Mode’ view to show by default.
Extend ‘Display Mode’ view
So far nothing interesting – the steps above you can find in many other posts. But this is not the result I was looking for. Now list items in display mode are represented text, but this is not the text I needed – I wanted to have ‘Yes (click edit to modify)’ or ‘No (click edit to modify)’.
In InfoPath you don’t have if-else statements to apply conditional formatting, but you can simulate it with rules. For example can create 2 calculated value controls, with text value, and then show/hide them based on the value of the Boolean field. For some reason I don’t like this approach. It works just fine, but if you have 5, or 10, or more Boolean fields to show in this way, you need to multiply them by 2 to get the number of the controls to include, and each control should have at least 1 rule attached to show/hide it. And then if you want to change the text displayed, you have to go through all controls and change them one by one. This is not flexible at all …
Instead decided to create a new data source, XML file, to keep mapping between field value and display value:
Then added this file as resource file to my form:
After that created a new data connection, to read from the XML file in form resources:
Once this was done, opened calculated field properties and selected to load ‘text’ field from the new data source. Then selected Filter Data options and created a filter where ‘value’ field is equal to my Boolean field value. As you can see in the XML schema above, ‘value’ property keeps ‘true’ or ‘false’ which are the text representatives for a Boolean field values in InfoPath.
If you get an error during design check, saying that non-repeating control cannot be bound to a repeating field or group, delete the calculated control and re-create it again. Looks like there is a problem with calculated control when changing its binding.
Now publish the form again, and check the result. In display mode the text is as I wanted it to be:
While in edit mode Boolean field can be edited as a checkbox.
No comments:
Post a Comment