Jul 14, 2008

Infopath - Switch View Depending on the Current User's Role

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 :-) 

You can download the web service (sources and solution package) on my CodePlex project "I Love SharePoint".

  • 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:

http://[host]:[port]/[site]/[subsites]/_vti_bin/ILoveSharePoint/UserService.asmx

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...

Have fun,

Christian

20 comments:

Johnny said...

Thx to you for this solution. It's very good and solves a lot of problems i had, when i wanted to use permission-handling in browser-based forms with approval-steps. Keep it up!
Greetings

Maneer said...

Thanks
That was great and works perfectly.
But i have a minor issue, if a user is part of multiple groups then your solution reflects the group name coming first alphabatically. Do you have any workaround for displaying multiple groups?

Maneer

Christian said...

Hi Maneer,
that's right. The service loops through all groups of the current user and adds them to the result.
What's your scenario?
Bye, Christian

galla said...

Great solution.but i have one problem please clarify.iam hiding the sections on InfoPath Form depending on UserGroups.It's working fine. but when i open the fublished form,the hiden sections are visible for all user's.It's might be data is not refreshing.please clarify me.

Christian said...

Hi galla,

My first idea is does Infopath Form Services supports to hide sections? Please check that you only use compatible controls. http://office.microsoft.com/en-us/infopath/HP100340241033.aspx Regards this link it should support sections. This isn't an service issue, this seems to be a browser form issue. You can try to change the controls postback behaviors in the control's properties.
Bye, Christian

Burak Donbay said...

Thanks for the article.
It's perfect!!!

.NET Dev said...

Great !!!
But I have one issue:
I added my active directory group in the sharepoint group. So, it is unable to detect the sharepoint group.

Any Solution?

Thanks
.NET Dev

Anonymous said...

How did you create this custom webservice? Did you use WSS 3.0 SDK?
Please let me know step by step. what components are needed to be installed?

Thanks

Christian said...

Hi,

read on the MSDN "Walktrough: Creating a Custom Web Service" http://msdn.microsoft.com/en-us/library/ms464040.aspx
Hope this helps.

Bye, Christian

Maneesh said...

.NEt Dev,
Please use following link to get AD groups for a user.
http://urenjoy.blogspot.com/2009/04/getting-active-directory-groups-from.html and use these AD groups as a member of sharepoint groups.

Winfried said...

Super WebService!

Ist es möglich einen beliebigen AccountName als Parameter zu übergeben? Ich möchte die Gruppenmitgliedschaften eines per ContactSelector ausgewählten Benutzers abfragen. Oder gibt es dafür eine Alternative?

Gruß

Winnie

Anonymous said...

I've got an 5566 event error: access is denied. What am i missing?

Alison said...

Hi,
Has anyone got this working on SP 2010 ?

Thanks

Akalanka said...

This works for sharepoint 2010

Naresh said...

great job!!! it helped a lot.

Anonymous said...

you're definetely the man!!! u justed saved my week :)

GREAT JOB!!!

Anonymous said...

Hi,

I am using your web service to display SharePoint Groups and then hide controls (Sections) depending on what group you belong to. I am using SharePoint 2010 and InfoPath 2010. It works fine when I am in preview mode in InfoPath, however, when I publish the form and open a new document in the form library, there are no groups showing.
Can you please advise?

Thanks

Padraic

Anonymous said...

Mine does not work at all. I am trying to diplay a view based on what group they are in and it seems it is not reading the user groups. I am basing it on a Form load rule if string is equal to Owners then show the Edit View and it is not working.

Mendo_Man said...

Brilliant solution, easy to install and use. Other solutions were either code or altering the form's source files. Thumbs up

Anonymous said...

How do I use this web service? I know how to add the data connection, but is there an example of how to actually utilize this solution in practice? I would like to load views based on groups on form load. Is there an example I can follow?