November 7, 2011

// // 8 comments

INFOPATH FORM HIDE/DISABLE FIELDS BASED ON SHAREPOINT GROUP MEMBERSHIP






Hi guys in this tutorial you will get to know how to identify whether a user is part of a Share point group in InfoPath form.


NOTE:

This will work only for WINDOWS AUTHENTICATED SP sites

In this example we will use the logged in persons user name is in IT SP group.
If he is in the IT SP group we will enable Submit Button and
If he is not in the IT SP group we will enable Submit to IT button.

STEP 1:

Go to the SP site.
Go to People and groups and create a group IT.
And Add users to the group.






Added test user 1 in the SP group.



STEP 2:

Open the InfoPath 2010.


Created 3 fields Name, Emp ID, Department and 2 Buttons Submit to IT and Submit



STEP 3:

Go to the Data tab and select Data Connections






Add a Data Connection
















 













STEP 4:
A data connection wizard box opens.

























Select create a new connection and Select Receive data
And Click Next

Select SOAP web service and click next
















 



Type the server url/ _vti_bin/userprofileservice.asmx and click next

For example http://spsite:2222/ _vti_bin/userprofileservice.asmx



















 Select the GetUserProfileByName web service and click next
























Click next and once again click next




















Finally click Finish.

We use this web service to get the current user name

STEP 5:

Again add a another data connection as SOAP web service (Follow the above steps in step 4)
















Type the server url/ _vti_bin/usergroup.asmx and click next

For example http://spsite:2222/ _vti_bin/usergroup.asmx


















Select the GetGroupCollectionFromUser web service and click next



















Click Set Sample Value button



















In Parameter Details add the domainname\user (SPSITE\prem) and click OK

Then click Next.


























Again do the above step
Click Set Sample Value button
In Parameter Details add the domainname\user (SPSITE\prem) and click OK


Then click Next.

Once again click Next



























Finally Click Finish

We use this web service to get SP group.

STEP 6:

Out of the box the GetGroupCollectionFromUser do not support our need.

So we need to do some changes in the xml make it work for our needs.

















Go to File > Publish and Export Source Files

In the Exported files open the GetGroupcollectionFromUser with large size file. In above selected us GetGroupCollectionFromUser File in wordpad to edit.

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

It will be in 3rd line. Add the following code below it.

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


STEP 7:

Find this code
<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>

And replace the above with this

<s:element name="GetGroupCollectionFromUser" type="tns:GetGroupCollectionFromUserType" />


Save the file.

STEP 8:


                      

Open the Manifest.xsn file in the Exported files.

NOTE: If the Manifest file is not opening (Showing error means, The Changes did in GetGroupCollectionFromUser is wrong, verify it once more)

Now the Schema of the GetGroupCollectionFromUser looks like this.



STEP 9:

 Go to Data >  Form Load.


Add a new action without any connection
Click Add >  Set a Field’s value.
Click the fields and select the GetGroupCollectionFromUser Data Source
























Select the userloginName field and click OK




















Now for the value click the fx button and click
Click the Insert field or Group and select the GetUserProfileName Secondary data connection from the dropdown.

























Select the Value and click Filter data.

And click Add new Filter

In the Specify Filter Conditions Select the first dropdown box which will have Value by default and then click Select the field or Group a window opens.























Select the GetuserProfilebyName data source and Select Name and click OK

And in Specify Filter Conditions for the third dropdown box type text as
AccountName



















First rule looks at the end.

STEP 10:



















Add second rule in form load and click query for data and select the Getgroupcollectionfromuser in data connection and click OK

STEP 11:

Add final 3rd rule in form load. Click and add to set a fields value in action.

For the field to set the value select the main data source
























Select the field Department and click OK

For the value for department goto the GetGroupCollectionFromUser
























Select the name field and click Filter Data…
















Add the filter for Name is equal to IT (The SP Group which I used)
Click OK



















Third rule looks at the end.

STEP 12:

Write the rule for Submit button to disable when department is equal to IT




STEP 13:

Finally everything is done.
When the user is in IT department the Submit to IT button is disabled.

For other users the button will be enabled.

Also clear the Department field in the Submit button to make it work for all times 




For any doubts do post the comments below

8 comments:

  1. This is simply awesome....:-)

    Please post many like this premkumar...

    ReplyDelete
  2. Hello,

    I m getting this error in the preview mode of InfoPath Designer 2010;
    The SOAP response indicates that an error occurred on the server:Server was unable to process request. ---> Attempted to perform an unauthorized operation.

    I get also this error in the site;
    An error occurred while trying to connect to a Web service.

    An entry has been added to the Windows event log of the server.
    Log ID:5566

    In the Log file;

    WebServiceQuery: End bbc433d3-ad1d-4a93-8adf-5b979da206d6
    04/11/2012 09:16:54.46 w3wp.exe (0x206C) 0x26B0 InfoPath Forms Services Runtime - Data Connections 7tfk Medium Data adapter failed during OnLoad: The remote server returned an error: (500) Internal Server Error. Server was unable to process request. ---> Attempted to perform an unauthorized operation. bbc433d3-ad1d-4a93-8adf-5b979da206d6
    04/11/2012 09:16:54.47 w3wp.exe (0x206C) 0x26B0 InfoPath Forms Services Runtime - Data Connections eq8l Warning The following query failed: GetUserProfileByName (User: serverName\userName, Form Name: Template, IP: , Connection Target: , Request: http://servername/sites/xxx/Lists/GroupList/AllItems.aspx, Form ID: urn:schemas-microsoft-com:office:infopath:list:-AutoGen-2012-04-10T15:51:08:78Z Type: DataAdapterException, Exception Message: The remote server returned an error: (500) Internal Server Error. Server was unable to process request. ---> Attempted to perform an unauthorized operation. The remote server returned an error: (500) Internal Server Error.)

    WebServiceQuery: End bbc433d3-ad1d-4a93-8adf-5b979da206d6
    04/11/2012 09:16:54.51 w3wp.exe (0x206C) 0x26B0 InfoPath Forms Services Runtime - Data Connections 7tfk Medium Data adapter failed during OnLoad: The remote server returned an error: (401) Unauthorized. bbc433d3-ad1d-4a93-8adf-5b979da206d6
    04/11/2012 09:16:54.51 w3wp.exe (0x206C) 0x26B0 InfoPath Forms Services Runtime - Data Connections eq8l Warning The following query failed: GetGroupCollectionFromUser (User: serverName\userName, Form Name: Template, IP: , Connection Target: , Request: http://servername/sites/xxx/Lists/GroupList/AllItems.aspx, Form ID: urn:schemas-microsoft-com:office:infopath:list:-AutoGen-2012-04-10T15:51:08:78Z Type: DataAdapterException, Exception Message: The remote server returned an error: (401) Unauthorized. The remote server returned an error: (401) Unauthorized.)


    How to solve this problem please ?

    ReplyDelete
  3. Hi

    Is your web application a form authenticated one ????

    ReplyDelete
  4. I need to switch views based on the persons sharepoint user groups. This is not working for me any help?

    ReplyDelete
  5. Hi Benjoe,

    Following the above example..Add another rule in form load.

    If the Department is = IT

    Switch view to IT.

    ReplyDelete
  6. the document .xsd is in use by another application and cannot be accessed, when trying to open the xsd file in wordpad

    ReplyDelete
  7. Hi jagadeesh,

    In Step 6 after

    Go to File > Publish and Export Source Files

    Close the infopath form and try editing the document.xsd

    ReplyDelete
  8. Thanks alot. It works great!
    However, I get one warning when starting a new form:

    The SOAP-reply tells there has been an error on the server:

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
    The parameter loginName cannot be empty of be longer than 251 characters.0x80131600

    Any suggestions?

    ReplyDelete