locked
Determining browser in use by user filling in an infopath form RRS feed

  • Question

  • Hi

    I was wondering if anyone could tell if it's possible from an Infopath form to determine what browser is being used by the user filling it in. Ideally I'd like to have the form switch views on opening if it is being filled in on an Ipad or Android Tablet.

    Thanks in advance

    Neil Elliker

    Thursday, February 9, 2012 5:16 PM

Answers

  • Hi Neil,

    There are a couple of options you have:

    The only option within the InfoPath Object Model is the "IsMobile" property of the Application.Environment object. If this is sufficient then in the Loading event, you can use code like this:

    if (Application.Environment.IsMobile)
         e.SetDefaultView("vwMobile");  //Where "vwMobile" is the name of your mobile view

    However, if this is not sufficient then the only other option would be to display the form in a custom ASPX page so you can write code behind the page to get the UserAgent information.

    To get this to work:

    - Add a new node to your Form Template (i.e. browserType) and set the Default Value of the node to: Desktop (This could be any value that makes sense to you - I just used Desktop for this sample)
    - Add the following code to the Loading event of the XSN:

    XmlNamespaceManager ns = this.NamespaceManager;
    XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
    string browserType = xnMain.SelectSingleNode("/my:myFields/my:browserType", ns).Value;
    if (browserType == "Mobile")
         e.SetDefaultView("vwMobile");

    - Follow the steps in this white paper to create a custom ASPX page (http://msdn.microsoft.com/en-us/library/aa701078.aspx - NOTE: these steps are valid for 2010)
    - Add this code to the "Initialize" event of the XmlFormView control on the custom ASPX page:

    if (Request.UserAgent.Contains("iPad"))
    {
         XmlNamespaceManager ns = XmlFormView1.XmlForm.NamespaceManager;
         XPathNavigator xnMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
         xnMain.SelectSingleNode("/my:myFields/my:browserType", ns).SetValue("Mobile");
    }

    What this will do is check the UserAgent string when the custom ASPX page is opened and set the "browserType" node in the XSN. Then as the new InfoPath form loads, it reads that node and switches to the appropriate view based on the value in the node.

    I hope this helps!

    Scott


    Scott Heim - Microsoft Office InfoPath and SharePoint Designer Online Community Support

    • Proposed as answer by Scott He - MSFT Friday, February 10, 2012 4:05 PM
    • Marked as answer by Wayne Fan Monday, February 20, 2012 11:01 AM
    Friday, February 10, 2012 4:05 PM

All replies

  • Hi Neil,

    The web browsers use a user-Agent value as following:
    Mozilla/[version] ([system and browser information]) [platform] ([platform details]) [extensions].
    http://en.wikipedia.org/wiki/User_agent The values are different from each other.
    You can use the “System.Web.HttpBrowserCapabilities” to get the browser information using Asp.net
    http://msdn.microsoft.com/library/3yekbd5b

    here is a demo about “How to programmatically change the default view of an InfoPath form”
    http://support.microsoft.com/kb/826997

    Thanks,
    Wayne Fan
    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact  tnmff@microsoft.com.


    Wayne Fan

    TechNet Community Support

    Friday, February 10, 2012 3:54 AM
  • Hi Neil,

    There are a couple of options you have:

    The only option within the InfoPath Object Model is the "IsMobile" property of the Application.Environment object. If this is sufficient then in the Loading event, you can use code like this:

    if (Application.Environment.IsMobile)
         e.SetDefaultView("vwMobile");  //Where "vwMobile" is the name of your mobile view

    However, if this is not sufficient then the only other option would be to display the form in a custom ASPX page so you can write code behind the page to get the UserAgent information.

    To get this to work:

    - Add a new node to your Form Template (i.e. browserType) and set the Default Value of the node to: Desktop (This could be any value that makes sense to you - I just used Desktop for this sample)
    - Add the following code to the Loading event of the XSN:

    XmlNamespaceManager ns = this.NamespaceManager;
    XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
    string browserType = xnMain.SelectSingleNode("/my:myFields/my:browserType", ns).Value;
    if (browserType == "Mobile")
         e.SetDefaultView("vwMobile");

    - Follow the steps in this white paper to create a custom ASPX page (http://msdn.microsoft.com/en-us/library/aa701078.aspx - NOTE: these steps are valid for 2010)
    - Add this code to the "Initialize" event of the XmlFormView control on the custom ASPX page:

    if (Request.UserAgent.Contains("iPad"))
    {
         XmlNamespaceManager ns = XmlFormView1.XmlForm.NamespaceManager;
         XPathNavigator xnMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
         xnMain.SelectSingleNode("/my:myFields/my:browserType", ns).SetValue("Mobile");
    }

    What this will do is check the UserAgent string when the custom ASPX page is opened and set the "browserType" node in the XSN. Then as the new InfoPath form loads, it reads that node and switches to the appropriate view based on the value in the node.

    I hope this helps!

    Scott


    Scott Heim - Microsoft Office InfoPath and SharePoint Designer Online Community Support

    • Proposed as answer by Scott He - MSFT Friday, February 10, 2012 4:05 PM
    • Marked as answer by Wayne Fan Monday, February 20, 2012 11:01 AM
    Friday, February 10, 2012 4:05 PM
  • Hi Scott

    Thanks for the info, the ipad doesn't show as mobile when I use the ismobile property on the loading event in InfoPath, so I will go for option two.

    Thanks

    Neil Elliker

    Friday, February 10, 2012 4:57 PM
  • Hi Neil,

    I wasn't sure if it would or not. So just a couple of things to keep in mind with option #2:

    - This will open a completely separate ASPX page displaying your form. For the code on the ASPX page to fire, your form will need to be opened *all the time* with this ASPX page. So if users are just opening a new form all the time and submitting the form, no worries. But if you have to open existing, saved InfoPath forms (and you need this code to execute) you will need to implement a way for the user to select an existing form and have it open in this ASPX page. This is a bit more involved so be sure to think about this if you need to open existing files.

    Now, I don't know if there is a way to check the browser/user agent when using the InfoPath Form Web Part but if this may be of interest then I can do a little research on that and see if its possible.

    Scott


    Scott Heim - Microsoft Office InfoPath and SharePoint Designer Online Community Support

    • Proposed as answer by Scott He - MSFT Wednesday, February 22, 2012 8:41 PM
    Friday, February 10, 2012 6:39 PM