Thursday, August 1, 2013

InfoPath 2010 Paging

Introduction
In this post I will try to show you how to implement a simple paging mechanism using InfoPath 2010. This approach is useful if you are considering a quick & fast solution to handle InfoPath Repeating Tables (or sections) that returns 100s of items without writing code behind and by following few simple steps.

 How to do it:
In this scenario I already have an InfoPath Form that has a sample Repeating Table that returns more than 300 items. I would like to configure it to show a 100 item on each page, below are the steps:
1- Add a new field on you Main Data Source as following:


2- Add 2 new buttons on the form to handle Previous & Next Pages:

3-  In the InfoPath Repeating Table (or Section) add a new column and inset a new Calculated Value Control from the Controls Ribbon. This Calculated Value will hold the Row Number of the data that you retrieve:

4- Open the newly added Calculated Value Property and update your Data Source using the Position() XPath:

5- Create a Rule for the Previous Page Button on Click as following:
Rule Name: Previous Rule
Condition: PageNumber != 1
Action: Set Fields Value of PageNumber = PageNumber - 1

6- Create a Rule for the Next Page Button on Click as following:

Rule Name: Next Rule
Condition: PageNumber <= Count(ID) / 100
Action: Set Fields Value of PageNumber = PageNumber + 1
Note:
  • Whenever you see the number 100 it represents the page size.
  • you can select any fields with the Count() function, in this scenario I used ID field.

7- Now the Tricky Part ... you need to create 2 new Formatting Rules on the Repeating Table as following:

Rule Name: Hide Rule 1
Condition (Expression): position() >= 100 * xdXDocument:get-DOM()/my:myFields/my:PageNumber
Check on: Hide this Control


Rule Name: Hide Rule 2
Condition (Expression): position() < 100 * (xdXDocument:get-DOM()/my:myFields/my:PageNumber - 1)
Check on: Hide this Control




And that all folks ... Run your form and start Paging :)

Article taken from:
http://blogs.technet.com/b/meamcs/archive/2013/03/17/infopath-2010-paging-without-coding.aspx

Tuesday, April 9, 2013

Alternating Color on Repeating Section

You need to apply two conditions for formatting alternating color:

1.Expression :count(preceding-sibling::my:group14) mod 2 = 1   and i set  shading as "Yellow"--it has not worked.

2.Expression :count(preceding-sibling::my:group14) mod 2 = 0   and i set  shading as "Blue"--it has worked.
 when i use any one of those(1 or 2) its not working.

Only the combination(1 and 2) works

http://www.infopathdev.com/forums/p/20430/70754.aspx

Sunday, April 7, 2013

Retrieve a row of a repeating table

Problem: You have a repeating table on an InfoPath form and want to be able to retrieve the values of fields that are located in one specific row of the repeating table.

Solution:

You can retrieve rows of a repeating table either by their position within the repeating table or by the value of a specific field within the repeating table.

1 - Create a new InfoPath form

2  - Add a Repeating Table with 2 columns ("field1" & "field2")

3 - Add a Text Box control to the view and name it contents

4 - Add a second Text Box control to the view and name it toSelect

5 - Add a Button control and add the following code to the Clicked event handler:

A) If you want by position:


Public Sub CTRL14_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            If String.IsNullOrEmpty(toSelect) Then
                toSelect = "1"
            End If

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[" & toSelect & "]", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())


        End Sub

B) If you want by field value:

        Public Sub CTRL15_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[my:field1 = '" & toSelect & "']", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


6 - Save and preview the form

If the form retrieves rows by position, enter a number in the toSelect text box and click on the button.

If the form retrieves rows by field value then enter a piece of the text that is the same as the value of one of the field1 fields in any row of the repeating table.

Loop through rows of a repeating table

Problem: You have a repeating table on an InfoPath form and want to sequentially retrieve each row of the repeating table

Solution:
You can use the XPathNodeIterator object to loop through rows of a repeating table.

1 - Create a new InfoPath form

2 - Add a Repeating Table control with 2 columns

3 - Add a Text Box control to the view of the form and name it contents. Select it's Multi-Line property.

4 - Add a Button control to the form and on it's Clicked Event Handler write:


Public Sub CTRL8_11_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.

            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim rows As XPathNodeIterator = mainDS.Select("/my:myFields/my:group3/my:group4", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()

            While rows.MoveNext

                sb.Append(rows.Current.SelectSingleNode("my:field4", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(rows.Current.SelectSingleNode("my:field5", NamespaceManager).Value)
                sb.AppendLine()
            End While

            mainDS.SelectSingleNode("/my:myFields/my:contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


5 - Save and preview the form

When the form opens, add a couple of rows of data to the repeating table and then click on the button. The Text Box will display all data you entered into the fields in the Repeating table separated by semi-colons.

Saturday, February 2, 2013

Retrieve design information for a control on a view

Problem: You have a drop-down box list box control on an InfoPath form. The drop-down list box contains manually entered items, which means that the values and display names are part of the view the drop-down list box is located on. You want to programatically retrieve these values and display names when the form opens.

You can use the OpenFileFromPackage() method of the FormTemplate object of a form to access the XSL file that is used to create controls on a specific view

To retrieve design information for a control on a view:

1 - Create a new InfoPath template and add a Drop-Down List Box control to View1

2 - Add the manual choices to Drop-Down List Box Control:

Value: 1
Display Name: Item 1

Do the same for items 2, 3, 4 & 5.

3 - Add the following code to the event handle of the Loading event of the form


Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs)
            ' Write your code here.

            Using stream As System.IO.Stream = Template.OpenFileFromPackage("View1.xsl")

                If stream Is Nothing Then
                    Return
                Else
                    If stream.Length = 0 Then
                        Return
                    End If
                End If

                Dim doc As XPathDocument = New XPathDocument(stream)
                Dim root As XPathNavigator = doc.CreateNavigator

                Dim dropdown As XPathNavigator = root.SelectSingleNode("//*[@xd:xctname = 'dropdown' and @xd:binding = 'my:field1']", NamespaceManager)

                Dim iter As XPathNodeIterator = dropdown.Select("//option", NamespaceManager)

                If iter IsNot Nothing Then
                    While iter.MoveNext

                        Dim value As String = String.Empty
                        Dim displayname As String = String.Empty

                        Dim valuenode As XPathNavigator = iter.Current.SelectSingleNode("@value", NamespaceManager)

                        If valuenode IsNot Nothing Then
                            value = valuenode.Value
                        End If

                        Dim iter2 As XPathNodeIterator = iter.Current.SelectChildren(XPathNodeType.Text)
                        iter2.MoveNext()
                        displayname = iter2.Current.Value

                        If displayname = "Item 4" Then
                            MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager).SetValue(value)
                        End If

                    End While

                End If
                stream.Close()
            End Using
        End Sub


4 - Save and build the project

When the form opens, Item 4 should be displayed as the selected item in the drop-down list box.

The OpenFileFromPackage() method of the FormTemplate object of a form allows you to open fiels that are stored in the form template (.xsn).

Once you have an XPathDocument object, you can use this XPathDoucment object to read the data from the XSL file and retrieve the design information for any control.

Saturday, January 26, 2013

Getting a specific list view as a data source in Infopath


1.       Create the view you intend to pull in SharePoint. Make sure to sort it and filter it the way you want it to appear in the drop down.
2.       Click on Settings->List Settings to get to the settings of the list you want to use.

3.       Scroll to the bottom where it shows views and click on the view that you want. This should be where you would edit the view.
4.       Go up to the URL and copy everything after List= (this is the GUID of the view).
5.       Place what you have in notepad.
6.        Replace %7B with { (this should be first part.
7.        Replace %7D with } (this should be the last part.
8.       Replace %2D with a –
9.       You should have something that looks like
{FCE658C5-54A5-4460-B9F4-6EE4CBF017D7}
Note:the numbers and letters are unique to your view but the formatting should be the same.
10.   Take the url of the site where the list resides and put it in front of the GUID
11.   Now you have the pieces to build your string. The format of the string is
<URL>/_vti_bin/owssvr.dll?Cmd=Display&List=<ListGUID>&View=<ViewGUID>&XMLDATA=TRUE
for example: 
http://site/_vti_bin/owssvr.dll?Cmd=Display&List={FCE658C5-54A5-4460-B9F4-6EE4CBF017D7}&View={B9BC3880-739B-4DA0-89A7-716775E755B7}&XMLDATA=TRUE
12.   You now have the string you need to create the data connection in Infopath.
13.   In Infopath you need to add a data connection.
14.   Create a new connection to receive data. Click OK.
15.   Choose XML document. Click OK.
16.   Paste the string you created in notepad into the Enter the location of the XML data file that you want to use as your data connection.
17.   Click next and okay until you are out of the wizard and you are ready to use your data source. If you are given a security prompt you can just click cancel.
18.   Note: everyone that uses the form must have read access (at least) to the list you are pulling from.

This article was taken from Michael Doyle from SharePoint Ninja

http://www.sharepointninja.com/Blog/Lists/Posts/Post.aspx?ID=13

New Certification - MOS Office 365


Ignore and clear all errors in a form before submit

Problem: You want to be able to submit an InfoPath form despite any data validation errors that the form may have, so you want to ignore and delete all validation errors before submitting the form.


You can use the DeleteAll() method of the Errors collection to clear all of the errors in a form:

To Ignore and clear all of the errors in a form before submitting the form:

1 - Create a new form template

2 - Add a Textbox control to the view nad make it a required field by selecting its Cannot Be Blank Property.

3 - Go to the Submit Option under File and select "Allow users to submit this form" check box, select the "Perform custom action code option, deselect the "Show the Submit button in both the ribbon and the info tab in the InfoPath Filler". Click on Edit Code.

4 - Add following code to the FormEvents_Submit() event handler


Public Sub FormEvents_Submit(ByVal sender As Object, ByVal e As SubmitEventArgs)
            ' If the submit operation is successful, set
            ' e.CancelableArgs.Cancel = False
            ' Write your code here.
            System.IO.File.WriteAllText("C:\Temp\myform.xml", MainDataSource.CreateNavigator().OuterXml)
            e.CancelableArgs.Cancel = False
        End Sub


You will need to give the form full trust in order to save it to disk

5 - Add a button control to the view and label it "Submit". Add the following code to the clicked event handler of this button


Public Sub CTRL2_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.

            If Me.Errors.Count > 0 Then
                Me.Errors.DeleteAll()
            End If

            Submit()

        End Sub

* The first three lines of code delete all of the errors that are in the Errors collection of the form, and the fourth line of code calls the FormEvents_Submit() event handler to submit the form.

6 - Save and build project

7 - Preview the form.

When the form opens, a red asterisk (*) should appear in the text box as an indication that it is a require field. Click the button.  The form should be submitted without warning you about any data validation errors.

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP