About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday, 19 November 2014

Sorting in InfoPath Repeating table through C-# code

There are few ways we can sort the data in InfoPath repeating table. However, there is no out of the box sorting mechanism in InfoPath. (I think it is doable but very tedious). Here is very simple code, we can sort the data with multiple fields (columns).

Generally, explicit sorting is not required if you bind data from Database, as we can include our sorting login in SQL query. What, if you are getting data from different sources. Here is the code.
I am assuming that, on button click we will sort the data in Repeating table.

public void btnSortData_Clicked(object sender, ClickedEventArgs e)


           // Replace with your respective xpath and field names

            String xpathRow =           "/my:myFields/my:ManageProjectViewWrapper/my:SubProjectsSum/my:SUBPROJECTS";

             // you can add as many fields as you can 
          String[] xpathField = { "my:subprojecTitle", “my:subprojecID” };

            XmlSortOrder[] order = { XmlSortOrder.Ascending,
            XmlSortOrder.Ascending };


            XmlDataType[] type = { XmlDataType.Text, XmlDataType.Text }; 

            SortData(xpathRow, xpathField, order, type);


public bool SortData(String row, String[] field, XmlSortOrder[] order, XmlDataType[] type)


            bool returnVal = true;


            {// you should have more than 2 rows for sorting 

                XPathNavigator myNamespace =

                XPathExpression myRow = myNamespace.Compile(row); 


            XPathNodeIterator myRowIterator = myNamespace.Select(myRow); 

                if (myRowIterator.Count > 2)

                {// Loop through parameters to add sorting. 

                    for (int i = 0; i < field.Length; i++)

                    {// Set the various sort parameters. 

                        XmlSortOrder myOrder = order[i]; 

                        XmlDataType myDataType = type[i]; 

                 XPathExpression myField = myNamespace.Compile(field[i]); 


                        // Add sort parameter to compiled row XPath. 

        myRow.AddSort(myField, myOrder, XmlCaseOrder.None, "",myDataType);


                // Select table and sorted node set.

                XPathNavigator myTable = myNamespace.SelectSingleNode(row,



                myRowIterator = myNamespace.Select(myRow); 

                // Delete unsorted rows. 

         XPathNavigator xnRangeFirst = myNamespace.SelectSingleNode(row +

                "[1]", NamespaceManager); 

           XPathNavigator xnRangeLast = myNamespace.SelectSingleNode(row +  

                "[last()]", NamespaceManager); 


                // Append sorted rows. 

                while (myRowIterator.MoveNext())



            }// end of try 


             returnVal = false;


            return returnVal;

Happy programming....

No comments:

Post a Comment