Quantcast
Channel: code.bkwDesign » .NET
Viewing all articles
Browse latest Browse all 14

Showing the Title and Date Submitted on an InfoPath form

$
0
0

A better title for this blog post might be, “Why I’ll never want to do enterprise development on the InfoPath platform”. Suffice it to say, I’m writing all this down in an effort to give back to the internet a portion of the knowledge I gleaned from it trying to hammer out this solution. InfoPath form designing is sleek and fun for a little while, until you need to do something simple.

I realize a new InfoPath form, at the time a user is filling it out for the first time, has not been saved to SharePoint, and so it’s final title or ID is going to be indeterminate. But, once you are viewing a saved form, it seems like to me, it should be fairly straightforward to show the actual name of the form – somewhere on the form. That way, when my management folk print out last quarter’s cost-saving suggestions that were submitted into our InfoPath library, they can readily see the “suggestion ID” and “date suggested” right at the top of the document. No. This was so insanely difficult, I’m embarrassed to admit that all of our cost saving suggestions should probably have started with the phrase, “in addition to stopping Brian from learning about InfoPath development, my cost saving suggestion is…”.

I spent some time in the VSTA (yeah, “what’s that?”) hoping to see something like this.Name or Forms.CurrentForm.Name or something like I would have expected from my experience with the paradigm of
Window Forms development. But.. no dice!

I did initially try adding a secondary datasource to the Form within InfoPath Designer. I was hoping that the little check-box, “Include data for the active form only” was going to help me retrieve the form’s associated meta record from the source form library:

However, this seems to cause no data to ever be returned. There’s only a thousand posts on InfoPathDev.com and StackOverflow filled with the bloodied foreheads of developers with the same issue. According to this InfoPathDev forum post, my problem is that I have spaces in my title field. Well, it’s not an option for me to go back and reformat all my title fields. I have a WorkFlow that wakes up and *does stuff* to the title field upon receiving a new submission. It was difficult to write that workflow. I’m NOT editing that thing again… no way do I need TWO cans of worms!

Unfortunately, I was going to have to figure out how to query our SharePoint server using the Client Object Model to get the data record associated with this stupid form. Luckily, I was already familiar with this model and had a library of code snippets standing by (a whole console app, actually) where I query my SharePoint server fairly regularly to divine certain field names, etc.

In a previous iteration of InfoPath Designer development, I had already made submitted forms appear locked down when being reviewed. I used a secondary “view” to achieve this. So, I had discovered how to put my query inside a “ViewSwitched” event handler. If the name of the view we had changed to was named “ReadOnly” (that’s the name I gave it), then it was safe to execute my CAML query and tease out the actual ID that had been assigned to the form – and display it in a special field that is only shown in the “ReadOnly” view.

 

Imports Microsoft.Office.InfoPath
Imports Microsoft.SharePoint.Client
Imports System
Imports System.Xml
Imports System.Xml.XPath


Namespace CostSaver_Suggestions
    Public Class FormCode
        ' NOTE: The following procedure is required by Microsoft InfoPath.
        ' It can be modified using Microsoft InfoPath.
        Private Sub InternalStartup(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Startup
            AddHandler EventManager.FormEvents.ViewSwitched, AddressOf FormEvents_ViewSwitched
        End Sub

        Public Sub FormEvents_ViewSwitched(ByVal sender As Object, ByVal e As ViewSwitchedEventArgs)
            ' execute a CAML query against the SharePoint server
            ' to re-locate this item’s other metadata that got created by a WorkFlow after
            ' the form was submitted


            If (Me.CurrentView.ViewInfo.Name = "ReadOnly") Then '
                Dim xpnav As XPathNavigator = Me.MainDataSource.CreateNavigator()
                Dim mySuggestion As String = xpnav.SelectSingleNode("/my:myFields/my:Suggestion", NamespaceManager).Value

                'MessageBox.Show(mySuggestion)

                Dim client As New ClientContext(ServerInfo.SharePointSiteUrl.ToString())
                Dim spList As List = client.Web.Lists.GetByTitle("CostSaver_Suggestions")

                Dim qry As New CamlQuery()
                qry.ViewXml = Me.FormulateCamlQuery(mySuggestion)

                Dim spListItemColl As Microsoft.SharePoint.Client.ListItemCollection = spList.GetItems(qry)

                client.Load(spListItemColl)
                client.ExecuteQuery()


                Dim fieldUser1 As Microsoft.SharePoint.Client.FieldUserValue = Nothing
                Dim fieldUser2 As Microsoft.SharePoint.Client.FieldUserValue = Nothing
                Dim field3 As DateTime = Nothing
                For Each li As ListItem In spListItemColl
                    fieldUser1 = li("Editor") 'always shows Developer’s name,
                                              'due to a WorkFlow that runs in impersonation mode

                    fieldUser2 = li("Author") 'shows actual user who submitted
                    field3 = li("Submission_x0020_Date")

                    Dim headerVal As String = String.Format("{0} - {1} - {2}", fieldUser2.LookupValue, li("Title"), field3.ToShortDateString)
                    'MessageBox.Show(headerVal)
                    xpnav.SelectSingleNode("/my:myFields/my:ReadOnlyHeaderInfo", NamespaceManager).SetValue(headerVal)
                Next

            End If

        End Sub

        'The SharePoint field name is referred to as 'Suggestion0' on the server.
        'I figured this out by navigating to the list properties on the server,
        ' clicking on the field name, and looking at the *URL* - a tip from Doug Ware at elumenotion.com
        Private Function FormulateCamlQuery(ByVal valueToSearchFor As String) As String
            Dim whereClause As String = "<Eq><FieldRef Name='Suggestion0' /><Value Type='Text'>{0}</Value></Eq>"
            Return String.Format("<View><Query><Where>{0}</Where></Query></View>", String.Format(whereClause, valueToSearchFor))
        End Function
    End Class
End Namespace

 


Viewing all articles
Browse latest Browse all 14

Latest Images

Trending Articles





Latest Images