Problems with programatically switching views

D

DGibson

I have a form that has two views for it, "Request view" and "Approval view".
There are user roles set for each view. I have an "Everyone" role with no
specification, and then an "Approver role" that has specific AD accounts
listed. I want to programatically test if someone changes views to the
Approval view that isn't in the Approver role and kick them back to the
Request view.

The problem I'm having is that it seems that the OnSwitchView script is
firing when the form is first opened, and it causes an error "InfoPath cannot
execute any view-related object model calls. The view is not ready." This
error I'm thinking is caused because of multiple switchview calls occuring
when the form opens, but I don't know why they are firing.

Here is the script I have for testing:
function XDocument::OnSwitchView(eventObj)
{
// Test for Approver role
var strCurrentRole = XDocument.Role;
var strCurrentView = XDocument.View.Name;

XDocument.UI.Alert(strCurrentView + " " + strCurrentRole);

if (strCurrentView = "Approval view")
{
if (strCurrentRole = "Everyone")
{
XDocument.UI.Alert(strCurrentView + " " + strCurrentRole);
XDocument.View.SwitchView("Request view");
}
}
}

When the form loads, I get the initial alert of "Request view Everyone".
Then the second alert will fire "Approval view Everyone". Then I get the
error message. The form view doesn't actually change, I just see the Request
view as expected, but something more seems to be happening behind the scenes?

Is the OnSwitchView function supposed to be processed when the form is
initially opened? And is there another method I can use to prevent any users
from manually switching views?
 
G

Greg Collins [InfoPath MVP]

You can't do it that way. A big compaint of mine: InfoPath will NOT allow you to switch views while switching views. Seems dumb to me because I can think of many times I've wanted to be able to do that.

Anyway -- a workaround: Turn off the view switching in the menus (see View Properties), and then use buttons to switch the views. Use conditional formatting to disable the buttons when you don't want a particular use to switch the view.

--
Greg Collins [InfoPath MVP]
Visit http://www.InfoPathDev.com


I have a form that has two views for it, "Request view" and "Approval view".
There are user roles set for each view. I have an "Everyone" role with no
specification, and then an "Approver role" that has specific AD accounts
listed. I want to programatically test if someone changes views to the
Approval view that isn't in the Approver role and kick them back to the
Request view.

The problem I'm having is that it seems that the OnSwitchView script is
firing when the form is first opened, and it causes an error "InfoPath cannot
execute any view-related object model calls. The view is not ready." This
error I'm thinking is caused because of multiple switchview calls occuring
when the form opens, but I don't know why they are firing.

Here is the script I have for testing:
function XDocument::OnSwitchView(eventObj)
{
// Test for Approver role
var strCurrentRole = XDocument.Role;
var strCurrentView = XDocument.View.Name;

XDocument.UI.Alert(strCurrentView + " " + strCurrentRole);

if (strCurrentView = "Approval view")
{
if (strCurrentRole = "Everyone")
{
XDocument.UI.Alert(strCurrentView + " " + strCurrentRole);
XDocument.View.SwitchView("Request view");
}
}
}

When the form loads, I get the initial alert of "Request view Everyone".
Then the second alert will fire "Approval view Everyone". Then I get the
error message. The form view doesn't actually change, I just see the Request
view as expected, but something more seems to be happening behind the scenes?

Is the OnSwitchView function supposed to be processed when the form is
initially opened? And is there another method I can use to prevent any users
from manually switching views?
 
D

DGibson

Ah ha! I didn't see that option in the view properties before. That's
exactly what I needed, rendering the script issue a moot point.

Thanks for the help Greg.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top