Did you ever need to switch the view depending on the current user's role in an browser enabled Infopath 2007 form? Good luck!
The process presents a very common business scenario, but unfortunately also a very tricky one. The local Infopath client has a built-in mechanism to do that, but there isn't any for browser enabled forms running on Infopath Form Services.
If you googel for a solution, you'll maybe find an article from the Infopath team. They suggest to use an XML file containing all rules and users and store it within the form template. That would work! But, the downside of this approach is that you have to change the form template every time a user's role changes. This is inapplicable for large scale enterprises!
So my idea was to switch the view on the load event of the form depending on the user's SharePoint group using SPContext. Sounds good, but doesn't work, because you aren't able to switch the view programmatically on the load event- just great!
Next try: Because I'm a smart ass;-) I've started to try the built-in SharePoint Web Service (usergroup.asmx) to get the user's groups. The "GetGroupCollectionFromUser" method returns an "xsd:any"-node, doesn't work with Infopath - tiresome! Okay, there would be a chance to get around this in trying to modify the automatically generated schemas from the wsdl in the XSN- I don't like that.
Now, I've had enough! I've just written my own SharePoint Web Service. It's a very simple service with only one method, returning the SharePoint groups from the current user. Quiet simple, but effective! The Web Service works exactly as any built-in SharePoint Web Service, expects that it works with Infopath :-)
- Execute the deploy script (deploy.bat). Don't get frightened, the script first tries to uninstall the solution. This will fail the first time you're running.
- Paste the following url in your browser to check that the service is running: http://localhost/_vti_bin/ILoveSharePoint/UserService.asmx
- You're done, now try it in Infopath:
- Open an existing form template or create a new one
- Extras->Data Sources->Add
- Choose new connection to receive data
- Enter the url of the service: http://localhost/_vti_bin/ILoveSharePoint/UserService.asmx -> next
- Select "GetGroupsFromCurrentUser" -> next -> next -> finish
- After that select Data Sources in the task pane and choose the one you've just created
- Expand the child nodes of dataFields until you find the UserGroups node
- Drag the node on your form and choose section with controls
- Now you're done. Click the preview icon...
The url pattern for the web service is as follows:
The [site] and [subsites] placeholders define the site you want to get the current user groups from.
You can use the service with the Infopath's rules, perhaps to:
- Switch the view depending on the user's SharePoint group on the load event of the form
- Show or hide controls depending on the user's group using conditional formatting
Any questions or ideas? Leave a comment...