K2 SmartObjects allows one to expose data from any type of back-end. Very seldom this data will be sorted. In most form technologies this does not pose a problem, but with InfoPath there is no simple way to sort the data before it is bound to a control on the form.
Below I will explain the process of sorting the data of an InfoPath Data Connection. It is a complex process and requires knowledge of XML, but is possible.
Sorting the data from a data connection bound to a Drop-down list box requires the InfoPath form’s XML to be edited manually. After making changes to the XML of the control the changes can very easily be overwritten when the form is designed from the InfoPath designer again. To avoid the changes being overwritten one can encapsulate the changes in a “Preserve Code Block”. Doing so will preserve your changes but will not allow any modifications to be made to this control using the InfoPath designer. Due to this it is recommended that you create your drop-down list control and configure all the rules, validation, etc. which you require for the control before attempting to modify the control’s XML. This drop-down list will be connected to the SmartObject.GetList method.
When you are satisfied that your drop-down list is working, save your InfoPath form's source files (File > Save As Source Files). Provide a folder location where the files will be saved and click OK.
Go into the folder and open up each view's XSL file where the drop-down list was added. Search for the drop-down's code by searching for the name of the control or the SmartObject method you bound to the control.
It will probably look something like this:
<select class="xdComboBox xdBehavior_Select" title="" style="WIDTH: 197px;" size="1" xd:xctname="dropdown" xd:CtrlId="CTRL251" xd:binding="xdXDocument:GetDOM("PurchaseRequest_Create")/dfs:myFields/dfs:queryFields/tns:ExecuteSmartObjectMethod/tns:ExecuteSmartObjectMethodInput/SmartObjectInput/InputProperties/CostCenter" xd:boundProp="value" value="" tabIndex="0">
< Rest of code >
Take the entire select tag and cut it from that section.
Create a new template at the bottom of the file (before end tag and after the final template tag </xsl:template>)
Give it a unique name, and add the attribute that will allow the code to be preserved:
<xsl:template match="my:CostCenterDropDown" mode="xd:preserve">
Paste all the code from the drop down into this section.
To enable the sorting you need to add the following tag in the xsl:for-each tag
<xsl:sort select="Name" order="ascending" />
This will ensure that the entries in the drop-down list are sorted. The select=<> define the field of the SmartObject to sort by.
Return to the section where you cut the drop-down list code and create a new DIV tag there.
Inside this DIV tag specify that it should load your newly created template with code preserving turned on:
<xsl:apply-templates select="my:CostCenterDropDown" mode="xd:preserve"/>
If you have more than one drop-down list control on this view which requires sorting, repeat this process, ensuring each template has a unique name.
Save the XSL and repeat for each view where the sorted drop-down list controls should appear.
Now you can Right-click > Design the Manifest.XSF file in the source folder with the InfoPath designer and save the form back to its original file name.
When viewing the form the section will not be editable and you will see the section is marked “Preserve Code Block”
Run-time it will display the drop-down and the GetList will be sorted.
I hope some of you will benefit from this post!
Wed, Mar 10 2010 11:00 AM