October 2008 - Posts - Hilary Stoupa
in

InfoPath Dev

Hilary Stoupa

October 2008 - Posts

  • URL Input Parameters 'Gotcha'

    Ah, input parameters. It is so nice to be able to open a form with some values already filled in, isn't it? Except, of course, when they don't work. And to say they are a bear to debug would be an enormous understatement.

    I was recently helping a forum user with a form using input parameters passed in from a URL. Every thing looked exactly right. But try as we might, we kept getting an error:
    Given Key Not Present error

    And the field we wanted filled from the parameter remained blank:
    Sad and blank field

    For purposes of demonstration, I'll modify this code posted on MSDN for the LoadingEventArgs.InputParameters property.

    My form code looks like this:

            public void FormEvents_Loading(object sender, LoadingEventArgs e)
            {
                try
                {
                    string vDept = e.InputParameters["Dept"];
     
                    XPathNavigator myNav = CreateNavigator();
                    myNav.SelectSingleNode("/my:TestForm/my:Department", NamespaceManager).SetValue(vDept);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
     
            }

    Now, you'll notice that unlike the MSDN sample, I've added a little bit of error handling. Without it, the input parameters fail silently. While that may be the preferred end result, while trying to figure out why your parameter isn't populating a form field -- well, without a try/catch, you'll end up weeping and smacking your forehead against your desk. We don't want that.

    Basically, all this code does is get the value of the input parameter, and set a form field to that value. To use the parameter, you'd add it to the URL for the template, like this:
    http://SharePointSite/FormLibrary/Forms/template.xsn?Dept=Accounting

    Sadly, follow this exactly and you will get the "Given Key not Present" error. Want to know why? Because on its way to your form, that parameter key got dropped to lower case. So did the value. So we think we are sending in 'Dept' and 'Accounting' but in fact we are sending 'dept' and 'accounting'. Modify the code to:

    string vDept = e.InputParameters["dept"];

    Guess what? No more error. Republish the form, and you can use the same input parameters from the URL above to open the form with the Department field filled:
    Populated field
    Do you notice the case of the word 'accounting'? The value passed was 'Accounting' -- and that is what lead me to guess that the parameter key was being changed to lower case as well.

    One last thing to keep in mind with URL input parameters -- be aware you are going to have to do some clean up if you are passing in strings like this. First off, you are going to lose any capitalization. Secondly, if you are passing in a string with spaces, be prepared to clean out all the '%20's.

Copyright © 2003-2017 Qdabra Software. All rights reserved.
View our Terms of Use.