ﻁ DevExpertise » Blog Archive » Creating a Wizard-Style Browser-Enabled InfoPath Form

DevExpertise

Practical tips and tricks for all things .NET, SharePoint, Silverlight, InfoPath, and general application development.

pas cher bactrim http://www.cricyt.edu.ar/?size=555582&pr... cetirizine prix
  • xeloda comprar en línea http://www.smwc.edu/?size=660127&price=6... coumadin venta libre
  • viagra otc http://kanab.utah.gov/licence.php?int=27... http://kanab.utah.gov/licence.php?int=27... commande priligy commande cetirizine ketoconazole prix
    http://crown.kings.edu/?size=693670&pric... http://crown.kings.edu/?size=200348&pric... viagra in farmacia
    viagra apotek koop kamagra http://www.dril-quip.com/cgi-bin/associa...
      kamagra livraison rapide achat viagra quebec http://www.cc-guingamp.fr/stat/glam/inde...
    cialis ci vuole la ricetta viagra billig online kaufen cialis foro

    Creating a Wizard-Style Browser-Enabled InfoPath Form

    Posted by DevExpert on February 16th, 2009

    InfoPath is a great tool when you need to build simple data-entry forms.  Although the tool is clearly limited when it comes to creating a rich user interface with complex controls, there are things you can do to enhance the experience if you’re creative.

    One of the ways various applications collect data is through the familiar wizard-style interface.  A wizard can be great if you need to collect a lot of information from user, but want to break it down into manageable chunks.  At first glance, InfoPath does not support such a thing, as there isn’t a fancy wizard control like there is in ASP.NET.  Luckily InfoPath allows us to create views, which provide a completely different display for the fields defined in the form.  With a little code and finesse, we can successfully implement a wizard-style browser-enabled InfoPath form.  The end result looks like the following:

    image

    The first thing to do is define the fields that are required in the form.  For the sake of this post, I built a very simple New User Wizard with a few fields, but in the real world a wizard is better suited for collecting a lot of data:

    image

    Now that the fields are defined, a view needs to be created for each “page” of the wizard.  In this case, I created 2 views in addition to the default view, and also renamed the default view to page1:

    image

    On the first page, I laid out the controls for the first page, and also included a Cancel button, Back button, and Next button.  I really don’t need a Back button for the first page, but i kept it in there as a placeholder and just disabled it by adding a conditional format.  In addition to the fields that the user needs to fill out, we have to perform our own data validation.  The reason is the validation doesn’t fire until the form is submitted, and we need to perform incremental validation just to ensure the first page’s fields are filled out correctly.  I’ll get to the code required to do this shortly, but for now I just added the errorMessage field, set the fill to transparent, removed the border, and disabled it:

    image

    Next, I created the second page of the wizard by cutting & pasting the first page and adding the appropriate fields:

    image

    Finally, I created the third page of the wizard, which for this scenario is just a summary page of previously filled-in data:

    image

    Now that the form UI is set up, we can start adding the rules and code to make it behave like a wizard.  Since I like my code nice and pretty, I renamed all of my buttons to btnCancel1, btnBack1, btnNext1, btnCancel2, etc.  For each cancel button, I added a rule to just close the form.

    For the back button, we don’t need to validate the fields, so the code required to jump back to the previous page is straightforward.  To switch views I used the SwitchView() method of the form’s ViewInfoCollection:

    public void btnBack2_Clicked(object sender, ClickedEventArgs e)
    {
        this.ViewInfos.SwitchView("page1");
    }
    
    public void btnBack3_Clicked(object sender, ClickedEventArgs e)
    {
        this.ViewInfos.SwitchView("page2");
    }

     

    Next I needed to implement the “Next” functionality, but before we switch the view to the next page, the fields on the current page need to be validated.  For this, I added the errorMessage field to each of the pages, and if the validation fails, I set the value of that field to the error message.  To do the validation and to set the field, I created two wrapper methods: GetFieldValue and SetFieldValue:

    private string GetFieldValue(string fieldName)
    {
        string xpath = string.Format("/my:myFields/my:{0}", fieldName);
        return MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager).Value;
    }
    
    private void SetFieldValue(string fieldName, string value)
    {
        string xpath = string.Format("/my:myFields/my:{0}", fieldName);
        MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager).SetValue(value);
    }


    Now for the Next buttons, I can verify data has been entered and if not, set the errorMessage field value:

    public void btnNext1_Clicked(object sender, ClickedEventArgs e)
    {
        if (!string.IsNullOrEmpty(GetFieldValue("firstName")) &&
            !string.IsNullOrEmpty(GetFieldValue("lastName")) &&
            !string.IsNullOrEmpty(GetFieldValue("emailAddress")))
        {
    
            SetFieldValue("errorMessage", string.Empty);
            this.ViewInfos.SwitchView("page2");
        }
        else
        {
            SetFieldValue("errorMessage", "Please fill out all required information");
        }
    }
    
    public void btnNext2_Clicked(object sender, ClickedEventArgs e)
    {
        if (!string.IsNullOrEmpty(GetFieldValue("workPhone")) &&
            !string.IsNullOrEmpty(GetFieldValue("cellPhone")) &&
            !string.IsNullOrEmpty(GetFieldValue("address")))
        {
    
            SetFieldValue("errorMessage", string.Empty);
            this.ViewInfos.SwitchView("page3");
        }
        else
        {
            SetFieldValue("errorMessage", "Please fill out all required information");
        }
    }


    The end result is something like this when an error occurs:

    image


    If all the data has been entered successfully, the last page is eventually visible:

    image

     

    Pretty slick, huh?

    6 Responses to “Creating a Wizard-Style Browser-Enabled InfoPath Form”

    1. Brian Says:

      Great tutorial. I did something similar using the buttons built-in rules but was having problems with multiple fields needing validation. You can only check up to 4 fields before the button would max out. This coded approach solved my dilema, thanks!

    2. Henry Staples Says:

      Thanks; helped me quickly put together a wizard example with validation structure.

    3. Mike T Says:

      Hi,

      I am using InfoPath 2003 and have tried to follow your example above to build a Wizard style form. I am not a very technical user and am a little confused about how and where you enter the code above. After I create a next button, I go to button properties and click edit form code, I get a page with a tab labelled script.js, a load of comments in green and a line reading

      function CTRL24_6::OnClick(eventObj)
      {
      }

      I assumed your code should go in between the {} but it doesn’t work I get an error

      InfoPath cannot open the selected form because of an error in the form’s code.
      The following error occurred:

      Expected ‘;’
      File:script.js
      Line:22
      private string GetFieldValue(string fieldName)

      Please can you point me in the right direction?

    4. Nik Patel Says:

      Is this InfoPath 2010? Can we implement similar feature on the SharePoint 2010 List forms (custom Infopath forms)? My understanding is you can’t write custom code on the SharePoint 2010 list forms. Is that correct?

    5. honee Says:

      hi!
      can i create wizard style forms using sharepoint web portal

    6. honee Says:

      hi!
      i am creating real estate web portal .i want to create wizard style web forms.i wanna know how sharepoint web portal will help me in doing this. i need an urgent reply

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>