Jithendrian

I need to use the infopath form in my windows application. So by using FormControl i am loading the infopath form. The thing is, the view inside the infopath form has to be loaded based on the person using the application. If the user is admin then his corresponding view has to be loaded.

So basically what i did is after loading the infopath form through FormControl i tried to change the view based on the user. The following is the code

private Microsoft.Office.InfoPath.FormControl frmCtrlInfopath;

string fileLocation,viewName;

fileLocation = System.Configuration.ConfigurationManager.AppSettings["FormLocation"];

this.frmCtrlInfopath.NewFromFormTemplate(fileLocation);

viewName = System.Configuration.ConfigurationManager.AppSettings["AdminView"];

this.frmCtrlInfopath.XmlForm.ViewInfos.SwitchView(viewName);

When i try to execute i get a COMException stating "InfoPath cannot execute any view-related object model calls. The view is not ready."

I need help regarding this. A sample code snippet would be helpful

TIA

Cheers

Jithendrian. R



Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

David Dean

If you are using the InfoPath rich client, you can get this functionality automatically using the User Roles feature, assuming that users are authenticated via Active Directory. The previous code snippet works when used within the code behind for the form template itself.

To make this logic work in your hosting form, the initial view needs to be set during the Initialize event for the FormControl.

Your class should implement the Microsoft.Office.InfoPath.IInitEventHandler event and set the window instance as the Initialize event handler in the form Load event.

Code Snippet

Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Set this class to handle the form control's Initialize event

myFormControl.SetInitEventHandler(Me)

End Sub

' Handler for the Initialize event of the InfoPath form control

Private Sub FormControl_Initialize(ByVal sender As Object, ByVal xmlForm As Microsoft.Office.InfoPath.XmlForm, _

ByRef viewsReadOnlyMode As Microsoft.Office.Interop.InfoPath.XdReadOnlyViewMode) _

Implements Microsoft.Office.InfoPath.IInitEventHandler.InitEventHandler

' Set the initial view

xmlForm.ViewInfos.Initial = xmlForm.ViewInfos("AdminView")

End Sub






Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi David,

i tried to implement the even handler you suggested. But then the initial view is set to the "AdminView" but when the form gets loaded it is loading with the default view but not the admin view. When i debug the code its showing that the form is set to admin view but its not getting reflected in display.

The following is the code snippet of the handler you suggested to implement

public void InitEventHandler(object sender, Microsoft.Office.InfoPath.XmlForm xmlForm, out Microsoft.Office.Interop.InfoPath.XdReadOnlyViewMode viewsReadOnlyMode)

{

viewName = System.Configuration.ConfigurationManager.AppSettings["AdminView"];

xmlForm.ViewInfos.Initial = xmlForm.ViewInfos[viewName];

viewsReadOnlyMode = Microsoft.Office.Interop.InfoPath.XdReadOnlyViewMode.xdDefault;

}


Need help on this.

TIA

Cheers

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi,

Any updates The issue is urgent.. need help on this at the earliest

Cheers

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

David Dean

Your best bet is still to add a Loading event to your form code and use the SetDefaultView method of the LoadingEventArgs parameter to specify which view should be initially displayed.

You can add code to the Initialize event handler on your hosting window to set an element in your form XML with the name of the initial view you want to display.

Alternatively, you can pass input parameters to the form (see http://blogs.msdn.com/infopath/archive/2007/02/26/passing-data-into-a-form-input-parameters.aspx for details) and access the input parameters in the form's Loading event to determine which view to display.

I am assuming with these suggestions that you are able to add code to the form template.






Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi David,

I tried what you suggested and also as specified in that blogs link. But then when i try to pass the input parameter from my Client Application when trying to load the infopath form i am getting a COMException with the following message

"InfoPath cannot create a new, blank form.
InfoPath cannot open the form. To fix this problem, contact your system administrator.

Form template: D:\CAB\InfoPath\MultipleViews.xsn usr=admin
The following form template for the form cannot be found:
file:///D:\CAB\InfoPath\MultipleViews.xsn%3fusr=admin"

I am trying to load the infopath form by the following code

this.frmCtrlInfopath.NewFromFormTemplate("D:\\CAB\\InfoPath\\MultipleViews.xsn usr=admin");

I have written the following code on the Infopath code behind to load which view.

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

// Write your code here.

string userType = e.InputParameters["usr"];

if(userType.Equals("admin"))

e.SetDefaultView("AuthorizerView");

}

But when i try to preview the form i am getting the following error message

System.Collections.Generic.KeyNotFoundException
The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.Office.InfoPath.Internal.ReadOnlyDictionary`2.System.Collections.Generic.IDictionary<K,V>.get_Item(K key)
at MultipleViews.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)

how to pass the input parameter when i run the infopath application directly

Need help on both the queries at the earliest.

TIA

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

David Dean

To include parameters when referencing a local file, make sure you use the file:// protocol correctly, e.g.:

file:///C:/CAB/InfoPath/MultipleViews.xsn usr=admin

In your example, you used a UNC-style path and InfoPath automatically escaped the question mark. You can type a URL into the Internet Explorer address bar to make sure the form template opens properly.






Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi David,

Thanks for the reply but then again i am getting the COMException Sad

InfoPath cannot create a new, blank form.
InfoPath cannot open the form. To fix this problem, contact your system administrator.

Form template: D:/CAB/InfoPath/MultipleViews.xsn usr=admin
The file name or path is not valid or it cannot be accessed.

I tried to type the same URL in the internet explorer but stil its not opening and got a message box asking me to make sure that the path or internet address is correct

Need help on this..

TIA

Cheers

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

David Dean

Did you include the file:// protocol identifier in front of the path




Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi David,

Once again thanks for the reply. But then its not working Sad

I am getting a COMexception with the following message

InfoPath cannot create a new, blank form.
InfoPath cannot open the form. To fix this problem, contact your system administrator.

Form template:
file://D:/CAB/InfoPath/MultipleViews.xsn usr=admin
The following form template for the form cannot be found: file:///D:\CAB\InfoPath\MultipleViews.xsn usr=admin

When i tried to run it from IE its opening but then its opening with the default view only Sad The Forms load event is not getting fired

Need help again on this

TIA

Cheers

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi All,

Any updates

TIA

Cheers

Jithendrian. r





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Jithendrian

Hi,

Any updates The issue is urgent.. need help on this at the earliest

Cheers

Jithendrian. R





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

Johnhodg

Just a thought but do you really need code for this

You can get the current username of the user via a built in function then have a rule that changes views automatically based on the user.

If you really wanted to use code you could have a element in your data source that you update with the user and have a rule that reacts to this as well. Just a thought.

JohnH





Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

David Dean

I agree with John's response.

The easiest way to do what you're trying to accomplish is to use a rule that conditionally executes on form open to switch to the correct view. If you need to look up the current user's role, you can create a simple web service that returns the role for the current user and execute that via a rule at form open, then add the rule to conditionally switch views based on the result.






Re: SharePoint - InfoPath Forms Services Changing Infopath Views Through FormControl

The Brebinator

Or perhaps a conditional button (with code) that opens the respective view based on the users role (you could pick this info up via the _vti webservice.

Just a thought.

Ivan