InfoPath, SharePoint

Cannot call InfoPath 2010 SwitchView on FormEvents_Loading

Yep, when you call this method on your FormEvents_Loading, it will give you COM error. I guess it have something to you with the ViewInfo that own the SwitchView are not properly loaded on Loading.

So this SwitchView cannot be used on FormLoading (or OnChange event handler)

this.ViewInfos.SwitchView("AuthorView");

The error will go like this on VSTA debug mode

Invalid Context: SwitchView cannot be called in the Loading event

or like this if you call it from InfoPath

System.InvalidOperationException
Invalid Context: SwitchView cannot be called in the Loading event.
   at Microsoft.Office.InfoPath.Internal.MomExceptionHelper.EnsureViewSwitchContext(XmlFormHost formHost)
   at Microsoft.Office.InfoPath.Internal.ViewInfoCollectionHost.SwitchView(String name)
   at MainForm.FormCode.FormEvents_Loading(Object sender, LoadingEventArgs e)
   at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnLoad(DocReturnEvent pEvent)
   at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnLoad(DocReturnEvent pEvent)

I’ve google for it; there’s solution from this MS support Microsoft Support site

They recomend to use FormEvent_Loading LoadingEventArgs attribute, so on loading you just have to do this

e.SetDefaultView("AuthorView"); // e is LoadingEventArgs 

It works!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s