Emulate User Roles in InfoPath Forms Services to Automatically Switch Views
Many of my SharePoint consulting clients and students express the need to have different users see different views of an InfoPath form. In the InfoPath client, this is easy to handle using the User Rolesfunctionality which has been well-documented elsewhere. Unfortunately, User Roles are not supported by InfoPath Forms Services for your browser-enabled forms. Here is a work-around that I have been using for quite some time that has worked well for me and my clients and students.
In this post we'll create a simple InfoPath form with two views: one view for most users and another view for administrators only. When the form loads, it will check to see if the current logged in user is an Administrator for that form and if he is it will display the Admin View to the user. If the user is not an administrator for the form, it will display the User View.
Create a Custom List to Store Users and Permission Levels
Although SharePoint exposes a number of web services that reveal security information and information about SharePoint Groups, I've never been able to get them to work reliably with InfoPath, especially without writing code. Since I can't use SharePoint Groups, I create a custom list to store the names of my users who will be administrators.
- Create a custom list called My Form Admins.
- Change the name of the Title column to Permission Level.
- Create a new column named User, of type Person or Group. Set the Showfield to User Name.
- Add a couple users.
- Your list should look similar to this:
Obviously, you'll need an InfoPath form to use this, so launch InfoPath and create a new blank form.
Within this form, create two views. Rename the default view to User View, name the other one Admin View. You don't have to add any fields on these views, but you can if you want. You will want to make them distinct so you know which view you are looking at, but it could be as simple as just putting the text "User View" and "Admin View" on the top of each view; that's what I did for this post.
Add Some Nodes to Store the Decisioning Information
In my form, I created a new group named AdminCheckingNodes, with two nodes both of type Text named Current UserUserName, and CurrentUserPermissionLevel. These will be used to store the User name of the current logged in user and his permission level, if one is set, in the My Form Admins list in SharePoint.
Add a Data Connection to the SharePoint List Containing the Admin Names
The form needs to be able to look at the My Form Admins list in SharePoint to determine the Permission Level of the current user. Create a data connection to this list.
- Click on Tools, Data Connections.
- Click Add.
- Create a new connection to Receive Data
from a SharePoint Library or List. - Paste in the path to the My Form Admins list.
- Select the My Form Admins list.
- Select both the Permission Level and User fields.
- Accept all the other defaults in the wizard and close the Data Connection window.
You need to write four rules to fire when the form first loads.
The first rule will store the name of the current user in the CurrentUserUserNamenode.
- Click on Tools, Form Options.
- Select the Open and Save category.
- In the Open Behavior section , click on the Rules button.
- Click the Add button.
- For the Rule Name, enter "Store Name of Current User".
- Click the Add Action button.
- Select the
Set a field's value action. - For the Field, select the CurrentUserUserName node.
- For the Value, click the Data Binding button. Click the Insert Function button, select userName. Click OK, and OK. Your form should look like this:
- Click OK. The completed rule will look like this:
- Click OK to close the Rules dialog.
- Click the Add button.
- For the Rule Name enter, "Clear out the current permission level". You need to do this just to make sure this field is empty in case the current user isn't listed in you're My Form Admins list.
- Click the Add Action button.
- Select the
Set a field's value action. - For the Field, select the CurrentUserPermisisonLevel node.
- For the Value, just leave the field blank. Click OK, and OK. Your form should look like this:
- Click OK. The completed rule will look like this:
- Click OK to close the Rules dialog.
- You should have two rules now. You're half done!
- Click the Add button.
- For the Rule Name enter, "Look up and store the current user's permission level".
- Click the Add Action button.
- Select the
Set a field's value action. - For the Field, select the CurrentUserPermisisonLevel node.
- For the Value, click on the data binding button.
- In the Insert Formula dialog, click on the Insert Field or Group button.
- Change the Data Source to My Form Admins (Secondary).
- Expand out all the nodes and select Permission Level.
- Click on the Filter Data… button.
- Click the Add button.
- In the first drop-down, select Select a field or group….Then select User, then click OK.
- Leave the second drop-down with is equal to.
- In the third drop-down, select Select a field or group….Then change the Data Source to Main, select the CurrentUserUserName node. Your condition should look lieke this:
- Click OK on the next four dialogs. Your form should look like this:
- Click OK three more times. Your rules should look like this:
- Click the Add button.
- For the Rule Name enter, "Switch to admin view if user is an admin".
- This rule will have a condition to check the permission level you stored with the previous rule. Click the Set Condition button.
- In the first drop-down, select Select a field or group….Then select CurrentUserPermissionLevel, then click OK.
- Leave the second drop-down with is equal to.
- In the third drop-down, select Type Text….Type the word "Admin" without the quotes; InfoPath will automatically add quotes for you. Your condition should look like this:
- Click OK.
- Click the Add Action button.
- Select the
Switch Views action. - For the view, select the Admin View, and click OK.
- Your completed rule will look like this:
- Click OK. All four rules should look like this:
- Click OK twice to return to your form.
Now you're ready to test the form! Woohoo! The easiest way to do this is to switch to the User View, then click the Preview button. If you are not listed as an Admin in the My Form Admins table, you shouls see the User View.
Close the form, add yourself as an Admin in the SharePoint list and then preview the form again. You should see the Admin view.
Conclusion
You can do a lot more with this besides just switch views. For example, depending on the permission level you set for the current user, the form may call different web services to populate certain fields of information. You could choose to show or hide different sections using Conditional Formatting. When users with certain permission levels submit the form, you may have a SharePoint Designer workflow check to see if it was submitted by an Administrator, and if it was, do something different than if it was submitted by a regular user. The uses of this are limitless. Let me know in the comments how you plan to make use of this.
Comments
Post a Comment