Microsoft Power Apps, SharePoint, & Microsoft 365 Consulting Company

Querying Security Groups in InfoPath with SharePoint Online

For those of you who have been using InfoPath long enough, you have likely had a request to pull information about a user. The user may be the current user or it may be a user within a field. In either case, doing some quick internet searches will point you to using web services of ‘getuserprofilebyname’ or ‘usergroup’.

That’s all well and good… except if you attempt to use either of these web services, you are greeted with an error “An error has occurred while trying to connect to a Web Service.” Per Microsoft, this is a result of a security measure called ‘loop back protection’. See here:

Now what?

Short answer: Javascript!

Longer answer, thanks to combining both of these sources:

http://sharepoint.stackexchange.com/questions/85146/how-to-get-user-security-group-name-through-javascript

+

https://spcloudguy.wordpress.com/2015/04/20/reading-user-profile-properties-from-infopath-forms-hosted-in-office-365/

The first link explains how to use javascript to get SharePoint security groups, but doesn’t mention how to use them in terms of InfoPath. The second link explains how to use javascript to get current user information and use it within the context of InfoPath. Put the two concepts together and you have the title of this blog post.

Steps to implement:

  1. Build your InfoPath form any way you want. This can be a list with a customized InfoPath form or using a Form Library.
  2. Add a text field to your list or library to store the SharePoint group. We chose the field name ‘SPGroup’.
  3. Add the field to the actual input InfoPath input form whether you want to or not! If you want to show it, then good for you and format as you please and don’t forget to set the field to read only. If you don’t like this, then a) set the text color to white b) remove the borders of the text box c) set the field to read only. That way people shouldn’t be able to see it or interact with it. You will see why you need this field on the form in the next step.
  4. Select the field and then open up ‘Manage Rules’ within InfoPath.
  5. Add a rule to ‘Disable the Control’ with no conditions which is essentially a dummy rule. Since the javascript loads after the form loads, you need a rule on the text box that will trigger the other rules. Once the javascript code loads, this rule will activate and in turn trigger all other rules.
  6. Right click on the field on the input form and click Text Box properties.
  7. Click the Advanced tab and add a ScreenTip label. We chose ‘SPGroup’. For those of you that are still wondering why we had to add the field to your input form, InfoPath won’t allow a ScreenTip label unless the control is actually on the input form.. and that ScreenTip is the primary reference in the code below.
  8. Publish the form.
  9. Click ‘New’ on the list or library.
  10. Edit the SharePoint page and add a Script Editor Web Part. If you are using SharePoint 2010, then add a Content Editor Web Part and apply formatting as necessary.
  11. Add the below code to the script editor and save.

You’re done! You can now use this SharePoint Group to apply formatting, validation or action rules to your other fields within InfoPath. Enjoy!