Search This Blog

Thursday, December 4, 2014

Checking if a user is a member in a SharePoint group within web InfoPath 2010 forms

First of all, you need to create a group and make sure that it can be viewed by Everyone.
You need to create to fields to grab the username and to check if he is a member in the specified group. I created them in a separate section and called them CurrentUser and IsGroupMember.
Now from the Data tab click Data Connection button.
Now we need to add a data connection from here to the user profiles service. Click Add… from the Connections box.
Select to “Receive Data” from your connection
Choose the “SOAP Service” from the wizard.
In the next dialogue box add the path to your sites UPS, it will be like this:
http://[YourURL]/_vti_bin/userprofileservice.asmx
It will grab the full URL by itself.
When the service is connected, it will ask for the query function. For our purposes, we will use GetUserProfileByName, like in the following screen shot.
In the next screen just click “Next”.
In the next screen just click “Next”.
If you want to change the connection name, do it from the next screen, else just click finish.
Go to the field we created called “CurrentUser”, right click, choose properties from the menu. Near the value field click the fx button to insert a formula.
Click “Insert Field or Group”.
From the dropdown, choose the user profiles data connection we created (GetUserProfileByName), expand data fields, expand tns:GetUserProfileByNameResponse > GetUserProfileByNameResult > PropertyData > Values > ValueData and choose Value from the list. Then click  Filter Data…
A Filter Data dialogue box will appear click Add…
From Specify Filter Conditions change the first drop down from value to Select a Field or Group
Choose the data source GetUserProfileByName Expand: dataFields > tns:GetUserProfilesByNameResponse > GetUserProfilesByNameResult > PropertyData > Name then click OK
From the Specify Filter Conditions the last drop down choose to type text and type: AccountName, exactly as you see it. Do not add quotes the system will add them.
Click OK on all open boxes.
Now we need to connect to the group web service. We will follow the same add data connection we did earlier for the User Profiles service, this time the URL of the service will be like:
http://[YourURL]/_vti_bin/UserGroup.asmx
From the next dialogue box choose: GetUserCollectionFromGroup then click next.
In the next two Dialogue box click  Set Sample Value…
Type the name of the group you need to check if the user is member in, and click OK.
In the next two Dialogue box click  Set Value…
Type the name of the group you need to check if the user is member in, and click OK.
Now from the InfoPath File menu, choose Publish from the right bar then click Export Source Files.
Save the files to the folder of your choice.
From the files choose GetUserCollectionFromGroup1.xsd and open it with your preferred text editor. I personally opened it with Notepad.
In the code file locate this code.
<s:importnamespace="http://www.w3.org/2001/XMLSchema"></s:import>
Type this snippet right under it:
 
<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>
 
In the same file locate this code:
<s:elementname="GetUserCollectionFromGroup"> 
  <s:complexType>
    <s:sequence>
      <s:elementminOccurs="0"maxOccurs="1"name="groupName"type="s:string"></s:element> 
    </s:sequence>
  </s:complexType>
</s:element>
Replace it with:
<!--<s:element name="GetUserCollectionFromGroup">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" type="s:string"></s:element>
</s:sequence>
</s:complexType>
</s:element>--><s:elementname="tns:GetUserCollectionFromGroup" 
type="tns:GetUserCollectionFromGroupType"></s:element>
Save the file and close it. Right click “manifest.xsf” and choose “Design”.
Now you will notice that the GetUserCollectionFromGroup will look like this:
Right click the second field we created IsGroupMember and choose Properties. From the properties box click fx button.
From the Insert Formula box click Insert Function…
From the Insert Function box choose the count function
Now double click on the text that says double click to insert field between the parenthesis.
From the Select a Field or Group box, choose GetUserCollectionFromGroup from the drop down. Expand myFields > queryFields > ns1:GetUserCollectionFromGroup > Users > User and choose :LoginName from the list, then click  Filter Data…
From the Filter Data box, click Add... to add a new filter.
From the Specify Filter Conditions dialogue go to the last drop down, choose Select a field or group… option
From the Select a Field or Group dialogue, choose “Main” from the drop down and navigate to CurrentUser field that we created earlier. Click OK.
Now your filter should look like this:
Click OK to all dialogue boxes.
This will grab the username you are logged in with and will check if you are a member in the group you specified. 
If you are a member in that group the field IsGroupMember will return 1, otherwise it will return 0.
Now you can create all types of rules to show/hide section, switch views… etc. based on the logged user group.

No comments:

Post a Comment