Byte My Data

A personal collection of advice and solutions I've used.

About the author

Something about the author

Get the number of attachments in an InfoPath attachment control

Original post from Jimmy Rishe: http://www.infopathdev.com/forums/p/9479/33529.aspx

Supposing you had a file attachment control bound to /my:myFields/my:group1/my:group2/my:fileField, where my:group2 is a repeating group, you can find the total number of file attachments in the group with

count(/my:myFields/my:group1/my:group2/my:fileField)

Note that this is only for explication and you don't have to type this all by hand.  In the InfoPath expression builder, you would just click "Insert Function...", select count, double-click the underlined part and select the attachment's field.

This would also count the number of file attachments where a file isn't attached, so to count only the attachments that have a file attached, you can use:

count(/my:myFields/my:group1/my:group2/my:fileField[. != '']) <-- note these are two single quotation marks '', not one double quotation mark "

You could also apply the "cannot be blank" property to the fields to ensure that they all have a file attached. Now if you want to add up the counts of file attachment fields in several repeating groups, you can sum them all up in an eval function,

xdMath:Eval(., "count(my:group1/my:group2/my:fileField1[. != ''])+count(my:group3/my:group4/my:fileField2[. != ''])+count(my:group5/my:group6/my:fileField3[. != ''])")

Note that in this case, the node paths do not begin with a forward slash, because they are all relative to the starting node (just a dot here, indicating the root node.).

You won't need to worry too much about the details if you use the buttons and dialogs to insert the functions and nodes.


Categories: InfoPath
Permalink | Comments (0) | Post RSSRSS comment feed

Get display name of logged in user in InfoPath

I needed to get the display name for the logged in user from active directory to populate a text box in an InfoPath form and went searching Google for an answer. Saddly to say, I fould some really complicated solutions that I didn't even want to attempt to deploy.

Luckily I found this article from jabit on CodeProject and it saved me a lot of (additional Smile) headache.
http://www.codeproject.com/KB/dotnet/UserName.aspx

For myself, I created two properties in the InfoPath code behind file and used it like this (I also had to add a reference to System.DirectoryServices.AccountManagement; you may too):

Imports System.DirectoryServices.AccountManagement
.
.
.
        Private GroupRequestor As String = "/my:myFields/my:groupRequestDetails"
        Private requestSubmittedBy As String = GroupRequestor & "/my:requestSubmittedBy"
        Private requestSubmittedByLogin As String = GroupRequestor & "/my:requestSubmittedByLogin"

        Private ReadOnly Property Fullname() As String
            Get
                Dim displayName As String
                Try
                    displayName = UserPrincipal.Current.DisplayName
                Catch ex As Exception
                    displayName = System.Environment.UserName
                End Try

                Return displayName
            End Get
        End Property

        Private ReadOnly Property Username() As String
            Get
                Return Application.User.LoginName
            End Get
        End Property

        Private Sub SetValue(ByVal node As String, ByVal value As String)
            Dim root As XPathNavigator = MainDataSource.CreateNavigator()

            root.SelectSingleNode(node, NamespaceManager).SetValue(value)
        End Sub

        Private Function GetValue(ByVal node As String) As String
            Dim root As XPathNavigator = MainDataSource.CreateNavigator()

            Return root.SelectSingleNode(node, NamespaceManager).Value
        End Function

        Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs)
            If GetValue(requestSubmittedBy) = String.Empty Then
                SetValue(requestSubmittedBy, Me.Fullname)
                SetValue(requestSubmittedByLogin, Me.Username)
            End If
        End Sub

Categories: .NET | InfoPath
Permalink | Comments (2) | Post RSSRSS comment feed

Currency data validation in InfoPath text box

Here's my custom pattern; it forces the user to enter the cents as well.

$?((\d{1,3}(,\d{3})*)|\d*)(\.\d{2})

Matches:
$1,000.00
1,000.00
1000.00
$1000.00


Permalink | Comments (1) | Post RSSRSS comment feed