programming4us
programming4us
WEBSITE

Microsoft SharePoint 2010 : InfoPath Forms Services - Responding to Events in InfoPath Forms (part 2) - Adding Code-Behind

11/20/2014 3:13:20 AM

Adding Code-Behind

You may have noticed in our demonstration form that no matter what is entered in the Company Name field, all companies are returned each time the Find Customer button is clicked. Because we’re using a database connection to retrieve our customer details, we don’t have the facility to pass in a parameter using the data connection interface in InfoPath. However, behind the scenes, InfoPath exposes a full object model that allows us to control practically all aspects of the data connection using managed code.

By adding a managed code event handler to our Find Customer button, we’ll dynamically modify the SQL query that’s used to generate our search results.

Note

Using managed code with InfoPath 2010 requires Visual Studio Tools for Applications. This feature can be installed using the Office 2010 setup program. Under Microsoft Office InfoPath, select Visual Studio Tools for Applications in the .NET Programmability Support group.


Before we start writing our custom event handler, we’ll modify the rules that we added earlier to prevent the database from being queried twice.

  1. Click the Find Customer button and then in the Rules pane, delete the Query using a data connection action: click the arrow to the right of the action and choose Delete.

  2. From the Developer tab in the ribbon, click the Language button. Make sure the Form template Code language is set to C#. (You can use VB, but the code in this sample is written in C#.)

  3. Switch to the Properties tab, and then click the Custom Code button in the Button section of the ribbon. The Visual Studio Tools for Applications interface will be loaded and a stub method will be created to handle the click event for our button.

  4. Type the following code in the body of the stub method:

    string xpath=@"/my:EquipmentRequest/my:Customer/my:CompanyName";
    XPathNavigator nav = this.MainDataSource.CreateNavigator();
    string companyName = nav.SelectSingleNode(xpath,this.NamespaceManager).Value;
    DataSource customer = this.DataSources["Customer"];
    AdoQueryConnection cnn = customer.QueryConnection as AdoQueryConnection;
    string allItemsQuery = cnn.Command;
    cnn.Command =allItemsQuery + " Where C.CompanyName like '%" + companyName + "%'";
    cnn.Execute();
    cnn.Command = allItemsQuery;


    A few items in this code sample require some explanation—first, the xpath variable: As mentioned earlier, InfoPath is an XML-based form designer. On the surface, this may seem trivial, since all developers are familiar with XML and the various objects in the .NET Framework that can be used to work with XML. However, because InfoPath is completely XML-based, things work a bit differently and it can take time to fully understand the difference.

    In traditional WinForms or ASP.NET programming, the presentation user interface is separate from the data. Often, most of the code that we write involves either reading data from or writing data to controls. Since the presentation layer in InfoPath is simply a transformation of the data (that is, an XSLT transform of XML data), there is no presentation layer to manipulate programmatically. Data is automatically bound to the appropriate controls. If we were writing an ASP.NET application, we may retrieve the value of the CompanyName text box by examining the Text property of the appropriate control; in InfoPath, there is no such control and we retrieve the value of the CompanyName field by querying the data model directly. The most common way to perform this query is to use XPath, and the xpath variable stores the XPath expression that refers to the appropriate field.

    Tip

    InfoPath Designer makes it easy to determine the XPath expression for a particular field. In the Fields pane, right-click the required field and select Copy XPath from the context menu. The XPath expression will be placed on the clipboard ready for use in custom code.


    The next item that warrants some explanation is the DataSource object. The following class diagram shows how data sources are defined by the InfoPath object model:

    You’ll notice that all of the classes in this diagram are abstract. All data connections are ultimately defined using XML schemas and other XML-based configuration data. At runtime, InfoPath creates objects from these files that derive from the appropriate base class. For example, in our code sample, we declare an object of type AdoQueryConnection to allow us to modify the query for our database connection. In reality, the actual type of this object is AdoQueryConnectionHost, which is an internal type that is created when the connection details are deserialized.

    Note

    Not all InfoPath forms can make use of managed code due to security restrictions in SharePoint. Custom list template forms and workflow forms that are automatically generated using SharePoint Designer can’t use managed code. When managed code is not available for a particular form type, the Developer tab will not appear in the ribbon.


    Now that our customer search function works properly, we can publish the form to SharePoint and check out the results.

Other  
  •  Microsoft SharePoint 2010 : InfoPath Forms Services - Accessing Data in InfoPath Forms (part 2) - Data Connection Libraries, Modifying UDC Files
  •  Microsoft SharePoint 2010 : InfoPath Forms Services - Accessing Data in InfoPath Forms (part 1)
  •  Extra-Curricular Activity - BMW 218d Active Tourer SE - An Engine That’s Fit For A Juke - Nissan Juke Tekna DIG-T (Part 1) - BMW 218d Active Tourer SE
  •  The Porsche Macan S Diesel 3.0 V6 – The Ultimate SUV You Can Drive (Part 2)
  •  Sharepoint 2010 : InfoPath Forms Services - InfoPath Overview (part 5) - Using InfoPath Forms in SharePoint - Creating Document Information Panels
  •  Sharepoint 2010 : InfoPath Forms Services - InfoPath Overview (part 4) - Using InfoPath Forms in SharePoint - Adding Formulae to Fields, Publishing a Form Template to SharePoint
  •  Sharepoint 2010 : InfoPath Forms Services - InfoPath Overview (part 3) - Using InfoPath Forms in SharePoint - Creating Form Templates
  •  Sharepoint 2010 : InfoPath Forms Services - InfoPath Overview (part 2) - BrowserForm Web Part
  •  Sharepoint 2010 : InfoPath Forms Services - InfoPath Overview (part 1)
  •  Sharepoint 2013 : List and library essentials - Sorting and filtering lists
  •  
    PS4 game trailer XBox One game trailer
    WiiU game trailer 3ds game trailer
    Top 10 Video Game
    -   Minecraft Mods - MAD PACK #10 'NETHER DOOM!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #9 'KING SLIME!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #2 'LAVA LOBBERS!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #3 'OBSIDIAN LONGSWORD!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Total War: Warhammer [PC] Demigryph Trailer
    -   Minecraft | MINIONS MOVIE MOD! (Despicable Me, Minions Movie)
    -   Minecraft | Crazy Craft 3.0 - Ep 3! "TITANS ATTACK"
    -   Minecraft | Crazy Craft 3.0 - Ep 2! "THIEVING FROM THE CRAZIES"
    -   Minecraft | MORPH HIDE AND SEEK - Minions Despicable Me Mod
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 92 "IS JOE DEAD?!"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 93 "JEDI STRIKE BACK"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 94 "TATOOINE PLANET DESTRUCTION"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 95 "TATOOINE CAPTIVES"
    -   Hitman [PS4/XOne/PC] Alpha Gameplay Trailer
    -   Satellite Reign [PC] Release Date Trailer
    Video
    programming4us
     
     
    programming4us