John Liu .NET

View Original

InfoPath FormServer - initial view and parameters via URL

 

Form Server provides quite a few very interesting Query parameters to allow us to influence how it will behave when rendering the form in browser.

The MSDN article is awesome and a great resource: How to: Use Query Parameters to Invoke Browser-Enabled InfoPath Forms

A really useful feature would be to allow the Query Parameter to control the initial view that the form opens in.  This allows us to control based on various stages of the form to render to different users different form "links" that takes them to different parts of the form.

 

Code Behind

MSDN has another great resource: InfoPath InputParameters Property

So if we add the InputParameters to the Form's load event, we can now specify the default view to open in.

 

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
    try
    {
        string view = "";

        if (e.InputParameters.ContainsKey("View"))
        {
            view = e.InputParameters["View"];
            e.SetDefaultView(view);
        }

    }
    catch (Exception ex)
    {
        SetValue("/my:myFields/my:FormSettings/my:ErrorLog", ex.Message + ex.StackTrace);
    }

}

 

Form Server URL

New Forms

http://server/_layouts/FormServer.aspx?XsnLocation=/FormServerTemplates/ProjectForm.xsn&SaveLocation=/Projects&Source=/Projects&OpenIn=Browser&View=Debug

Form Server URL query parameters not recognized by Form Server are passed to the form as Input Parameters

Existing Forms

http://server/_layouts/FormServer.aspx?XmlLocation=/Projects/413.xml&Source=/Projects&OpenIn=Browser&View=Debug

 

ViewInfos.SwitchView is not good

If you try to use ViewInfos.SwitchView - you'll probably get this error:

InfoPath cannot execute any view-related object model calls. The view is not ready

Hence, use the SetDefaultView method on the Load event args.

 

That's all, have fun!