We have a lot of requirements to check if user is in some SharePoint group then show some section in the InfoPath form.
But we don't like to use custom C# code to implement it since some security reason, here is the infopath form result which get all the SharePoint group for current use:
This article will show you step by step for how to do it.
1. First create a list to test it, needn't to add any cloumn since we only need to use its custom infopath form
2. Open the list, click the list tab, click "Customize Form"
3. In the opened InfoPath form, add data source for UserGroup.asmx (we need to use function GetGroupCollectionFromUser):
a. Click on Manage “Data Connections” which is on the bottom preview of “Field” pane.
b. Click on “Add” Button.
c. Check on “Receive Data” which belongs to “Create new connection to”.
d. Check the “Soap Web Service”.
e. Put this URL “http://servername/_vti_bin/userGroup.asmx?WSDL” in text bar.
f. Click on “Next” Button.
g. Select “GetGroupCollectionFromUser” from the list.
h. Click on “Next” Button.
i. Click on “Set Sample Value” Button.
j. Provide the value (domain\username, that is only a example data).
k. Click on “Next” Button
l. Click on Next Button
m. Check on “Store a copy of the data in the form template”.
n. Do not tick on “Automatically retrieve data when the form is opened”.
4. Add data source for UserProfileService.asmx (we need to use function GetUserProfileByName)
a. In the “Data Connection” window click on “Add” Button.
b. Check on “Receive Data” which belongs to “Create new connection to”.
c. Check the “Soap Web Service”.
d. Put this URL “http://servername/_vti_bin/ UserProfileService.asmx? WSDL” in text bar.
e. Click on “Next” Button.
f. Select “GetUserProfileByName” from the list.
g. Click on “Next” Button.
h. Click on “Next” Button by leaving “Set Value” of “account Name”.
j. Check the “Store a copy of the data in the form template”.
k. Do not check on “Automatically retrieve data when the form is opened”.
l. Click on “Close” Button in “Data Connection” window pane.
m. Go to “File” tab which is on top preview.
5. Publish the InfoPath(click the quick publish button)
6. Update the schema of the InfoPath form:
a. Click the file-> publish -> "Export Source Files"
b. Select one folder(e.g. D:\SharePoint\Candidates) for it
c. Open the GetGroupCollectionFromUser1.xsd(around 24 kb), close the SharePoint designer before updating this file.
d. Identify this line.
<s:import namespace="http://www.w3.org/2001/XMLSchema"></s:import>
e. Paste the follow xml blow the above line
<s:complexType name="GetGroupCollectionFromUserType"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="userLoginName" type="s:string"/> <s:element minOccurs="0" maxOccurs="1" name="Groups"> <s:complexType> <s:sequence> <s:element maxOccurs="unbounded" name="Group" > <s:complexType> <s:attribute name="ID" type="s:unsignedShort"></s:attribute> <s:attribute name="Name" type="s:string"></s:attribute> <s:attribute name="Description" type="s:string"></s:attribute> <s:attribute name="OwnerID" type="s:unsignedByte"></s:attribute> <s:attribute name="OwnerIsUser" type="s:string"></s:attribute> </s:complexType> </s:element> </s:sequence> </s:complexType> </s:element> </s:sequence> </s:complexType>
f. Search the follow xml
<s:element name="GetGroupCollectionFromUser"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="userLoginName" type="s:string"></s:element> </s:sequence> </s:complexType> </s:element>
g. And replace it with
<!--<s:element name="GetGroupCollectionFromUser"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="userLoginName" type="s:string"></s:element> </s:sequence> </s:complexType> </s:element>--> <s:element name="GetGroupCollectionFromUser" type="tns:GetGroupCollectionFromUserType" />
h. Save the xsd file and right click "manifest.xsf" select design(it will bring you back to the InfoPath form design UI)
7. Query data from UserProfileService.asmx
a. Click the data tab and click "Form Load" button
b. Click new to crate a new rule, name the rule as QueryUserProfile
c. Click Add for "Run these action" and select "Set a field's value", select the AccountName for the field:
d. Set the value
e. Click Add for "Run these action" and select "Query for data", then selec the GetUserProfileByName for the data connection:
8. Query data from UserGroup.asmx
a. Click the data tab and click "Form Load" button
b. Click new to crate a new rule, name the rule as QueryGroup
c. Click Add button "Run these action", select "Set a field's value", then select the GetGroupCollectionFromUser->myFields->queryFields-> tns:GetGroupCollectionFromUser->userLoginName for the field:
d. Set the value:
Click every OK after step 11:
e. Click Add button for "Run these actions" again and select "Query for data", and select the GetGroupCollectionFromUser
9. Add a list box to display the SharePoint groups:
a. Click the Home tab, and click "List Box"
b. Right click the added list box and select property
c. Select "Get choices from an external data source"
d. Select the GetGroupCollectionFromUser as data source
e. Select the entries, value and display name as below:
10. The last step is click the quick publish button again