Friday, May 18, 2007

Switching View in Infopath form from WebForm host

Infopath browser enabled forms offer a lot of flexibility and productivity. But with all the moving pieces involved in development and deployment of:

"Browser enabled - administrative approved - full trust - deployed to Sharepoint as feature-hosted in a web part" forms,

encoutering some simple glitches can cause some trouble. A solution to one of these small thing is posted here...

The scenario: You have a form with multiple view but want to invoke switch view from an ASPX hosting enviroment..

Solution:
1. Enable toolbars in your form (Tools-> Form Options -> Browser-> ShowToolbars)
2. Deploy it to a site collection and enable Web Page viewing from advance option in Form Template Library
3. Create a hosting environ. Use XML Form View control in your Web part or on your ASPX running under Form Services context.
4. Set the XsnLocation on the FormViewer control and open in browser.
5. View html source of the generated web page and you will find
"Toolbar.HandleViewSwitching(this)" being called on on change of a dropdown.
6. That's it !!! now go back disable the tollbars from your form and redeploy.
7. This time the drop down will not be rendered, hence no code to switch views.
8. Implement some code to spit out a drop down to you html. (You can do that in render method of you web part or pre render of your page, what ever makes more sense in your implementation)
9. Now istead of calling "Toolbar.HandleViewSwitching(this)" with this just pass in a reference to your drop down !!!

Here is a sample code:


<script type="text/javascript">
function ChangeView(viewName)
{
var dd = document.getElementById("FakeDropdown");
dd.value=viewName;
Toolbar.HandleViewDropdown(dd);
}
</script>
</span>
<select id="FakeDropdown" style="DISPLAY: none"> <option value="'View">View1</option> <option value="'View2'">View2</option></select>

<div onclick="ChangeView('View2')">Click Me</div>

This instructs the in memory from viewer script to think that view change is requested.