ProjectPoint

How To: Check if a user is part of a SharePoint group in InfoPath

Earlier this week a colleague sent out an email with the following problem:

“My client wants to hide certain fields and sections in an InfoPath form if a user is not part of a specific SharePoint group. The InfoPath form should not use code as the client declines solutions with code behind.”

This email got send around and a couple of people replied that custom code in form of a web service will need to be used. Not quite what my colleague’s client wanted.

One of the responses was to use the GetUserCollectionFromGroup method from the UserGroup web service (one of SharePoint’s out of the box web services), wrap it in a custom web service and verify the group membership in the custom web service. The reason for this suggestion was that the returned result set in InfoPath doesn’t get interpreted correctly. It basically can’t be used and the custom web service can be used to transform the results and do the verification within the web service.

I am a BIG fan of SharePoint’s out of the box web services and I was sure that the web service still can be used to get users of a certain SharePoint group. This could then be used to determine if the user is part of the group. My first thought was to save the InfoPath form as source files and modify the XML schema of that web service somehow. A bit of googeling later I had a solution ready to go thanks to Ian’s Blog entry here. It describes exactly what needs to be modified in the XML schema. Great post! And here is my solution with Ian’s help:


At this stage the form queries the current user’s account name and stores it in the CurrenUserAccountName field on form open. Next we will query the UserGroup web service to get us a list of users in a specific SharePoint group:

Now the tricky part. We need to save our form and then use the Export Source Files option under “Publish“.


Once that is done, close the form and navigate to the location where you exported your form to. Open the file named “GetUserCollectionFromGroup1.xsd“. This file defines the XML schema. Open the file in a text or XML editor. The following steps can be found in Ian’s blog. Thanks again!

Right under this code within the file:

1
<s:import namespace="http://www.w3.org/2001/XMLSchema"></s:import>

insert the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<s:complexType name="GetUserCollectionFromGroupType">
 <s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="Users">
   <s:complexType>
    <s:sequence>
     <s:element maxOccurs="unbounded" name="User" >
      <s:complexType>
       <s:attribute name="Notes" type="s:string"></s:attribute>
       <s:attribute name="Name" type="s:string"></s:attribute>
       <s:attribute name="IsSiteAdmin" type="s:string"></s:attribute>
       <s:attribute name="Sid" type="s:string"></s:attribute>
       <s:attribute name="ID" type="s:string"></s:attribute>
       <s:attribute name="LoginName" type="s:string"></s:attribute>
       <s:attribute name="Email" type="s:string"></s:attribute>
       <s:attribute name="IsDomainGroup" type="s:string"></s:attribute>
      </s:complexType>
     </s:element>
    </s:sequence>
    </s:complexType>
  </s:element>
 </s:sequence>
</s:complexType>

Now locate the following piece of code within your file:

1
2
3
4
5
6
7
<s:element name="GetUserCollectionFromGroup">
  <s:complexType> 
    <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="groupName" type="s:string"></s:element>
    </s:sequence> 
  </s:complexType> 
</s:element>

and replace it with this:

1
2
3
4
5
6
7
8
<!--<s:element name="GetUserCollectionFromGroup">
  <s:complexType> 
    <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="groupName" type="s:string"></s:element>
    </s:sequence> 
  </s:complexType> 
</s:element>-->
<s:element name="GetUserCollectionFromGroup" type="tns:GetUserCollectionFromGroupType" />

We are almost there J Now save the file and open the manifest.xsf by right clicking it and select Design.

If you have a look at your GetUserCollectionFromGroup data source and its fields, you will notice that it has changed from this:

to this:

Now the last thing we need to do is setup the IsGroupMember field. We want it to return a 1 if the user is in the group and a 0 if the user is not in the group.

We are done. You can now use the “IsGroupMember” field to hide and show different sections and fields in your form based on if the user is a member of the configured SharePoint group.

One thing to be aware of though is, that whenever you change the “GetUserCollectionFromGroup” data source, you will need to go through the setup process again for this data source.

Happy InfoPath-ing :)

*****UPDATE*****
Thanks to Phillip for the hint. Apparently you have to make sure that all users have read permissions to the SharePoint group you want to check. Otherwise it will come up with the common 5566 error that indicates that there are problems accessing the datasource. To give users read permissions on the group follow the steps below:

  1. Go to Site Actions -> Site Settings -> People and Groups
  2. Click on the “Groups” heading on the left
  3. Locate the group your users need to have read access to
  4. Click on the Edit button next to it
  5. Make sure that in the Group Settings section of the following page, for the first question (“Who can view the membership of the group?”) the option “Everyone” is selected
  6. Click OK

Thanks again to Phillip for pointing this out! Much appreciated.

, , , ,

102 thoughts on “How To: Check if a user is part of a SharePoint group in InfoPath
  • Karin says:

    In this step:
    In the next screen select Set Sample Value and provide InfoPath with the name of the group you want to query (e.g. if your user is in a SharePoint group “Test” put in “Test“)

    I put in my group name and I get a soap error saying it can’t find the group.
    Any idea. I have searched all afternoon and can’t find a resolution.
    I copy and pasted the group name so I know I did not make a typo.
    Please help. Everything else is working – even the changing of the code in the xml.
    Thanks,
    Karin

  • Karin says:

    I got it all to work in “Preview”, but when I publish it, it does not work. Anyone have any experience with that?? Pls let me know, Karin

1 2 3

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Visit Us On TwitterVisit Us On Linkedin