Monday, May 30, 2011

Getting the Current User's SharePoint Group Name - SharePoint 2010, InfoPath 2010

Since I did not write a post about SharePoint 2010 for some time, thought to write a pretty interesting post about how to get the logged in user's SharePoint group name using InfoPath functionalities. And for those who don't like to do some coding to get the thing done, they will surely like this approach. Since this is going to to be a longer post, I think it's better to go as a step by step approach, so I will start from Step 01.

Step 01.

Select the Custom List and on Ribbon under List click Customize Form. Then the List will be opening in the Microsoft InfoPath Designer 2010 .

Step 02.

Click on Manage Data Connections in InfoPath Designer. Then add two receiving data connections as following.
  • First we will add a receiving data connection for web service UserProfileService.
Receive Data Connection for UserProfileService
SOAP Web Service
  • In the text box type, http://ServerName/_vti_bin/UserProfileService.asmx.
UserProfileService.asmx?WSDL
  • From the list of web methods, select GetUserProfileByName.
Web Method - GetUserProfileByName
  • Click Next and you'll be asked for set the value for Account Name, leave it blank as doing it would get the current User.
AccountName - Leave it blank
  • Tick Automatically retrieve data when the form is opened.
Tick - Automatically retrieve data when the form is opened
  • Click Finish.
  • Then let's start creating the receiving data connection for web service UserGroup.
Receive Data Connection for UserGroup

SOAP Web Service
  • In the text box type, http://ServerName/_vti_bin/UserGroup.asmx.
UserGroup.asmx?WSDL
  • From the list of web methods, select GetGroupCollectionFromUser.
Web Method - GetGroupCollectionFromUser
  • Click Next. You will be asked to set sample value for UserLoginName. In that screen click Set Sample Value and in the appearing box type domain\Administrator.
userLoginName - Domain\Administrator
  • Click OK and Next. In the appearing screen Untick Automatically retrieve data when the form is opened.
Untick - Automatically retrieve data when the form is opened
  • Click Finish. Now you have successfully created two receiving data connections.

Step 03.

Now we will have to modify the xml schema for GetGroupCollectionFromUser data connection. For that we we will need to extract the InfoPath form. To extract the form, in InfoPath form go to File and Publish and then Export. Select a Folder to export the files and the form will be extracted.

Export

Step 04.

Now when the form has finished exporting, go to the folder you have selected. You will see a list of files that has been created in Export process.  But in our case we only focus on GetGroupCollectionFromUser.xsd (the xml schema for GetGroupCollectionFromUser data connection).

Exported Files
  • First Close the InfoPath Designer. Because you can't edit files when are being used by the InfoPath designer.
  • Open GetGroupCollectionFromUser1.xsd in a text editor (I prefer Notepad++) and edit it as below.
    • Find the following line. Normally it's the 2nd line of the file.
                <s:import namespace="http://www.w3.org/2001/XMLSchema">
    • Add following below 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>
    • Find the following part.
<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>
    • Replace it with the following code.
<s:element name="GetGroupCollectionFromUser" type="tns:GetGroupCollectionFromUserType">
</s:element>



Step 05.

Now right click on the manifest.xsf and choose Design. Choose GetUserProfileByName data connection and apply rules on the PropertyData field as following.
  • In Fields bar click on Show Advanced view.
Advanced view of Fields
  • Under Fields Select GetUserProfileByName (Secondary).
  • Then Add a rule. 
GetUserProfileByName (Secondary)
  • The rule condition is Name="AccountName", rule type is Action.
Select a field or group
GetUserProfileByName - Name
Type Text
    • Type "AccountName".
Type "AccountName"
  •  The rule actions are:
    • Set field’s value. Under Field, Set Field as follows.
GetGroupCollectionFromUser (Secondary) - Name
    • Then Under Value, Set Value as follows. Click fx and then Insert Field Or Group.
GetUserProfileByName - Value
    • Don't Click OK yet. Click Filter Data. The Click Add.
Select a field or group
GetUserProfileByName - Name
    • Click OK.
Type Text
Type "AccountName"
    • Click OK. Then you will get something like this.
Complete Formula (fx)
    • Click OK.
    • Then add another Action. Action is Query for data from GetGroupCollectionFromUser. 
Query for data - GetGroupCollectionFromUser
    • Click OK.
Step 06.

Now create a formatting rule for a field that you want to disable/hide if current user is not member of a given SharePoint Group. Put a condition as below, where the "LCGD Clerk" is the SharePoint Group Name.

Select a field or group
  • Set the Name in GetGroupCollectionFromUser (Secondary Data source).
GetUserProfileByName - Any occurrence of Name
Type "LCGD Clerk" (SharePoint Group Name)

Step 07.

Now Everything is completed. Final step is Publish the form.

Hope you all got a good idea about how to get the Current User's SharePoint Group Name using InfoPath functionalities. Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

45 comments:

  1. Waw. Amazing article.

    ReplyDelete
  2. I am able to get the logged-in user and user group information to display on my form. However, I can't seem to get the field conditional formatting to fire.



    This is my rule condition:

    Any occurence of Name is not equal to "Approver Group" or

    Number of occurences of Name = 0

    Disable Control



    It doesn't seem to matter if I login with a user who is in the Approver Group or not, the control is always disabled. Again, I have the user info and group displaying on page (for debugging purposes) and I can see that the logged in user is in the Approver Group.

    Any ideas?

    ReplyDelete
  3. hello, I would suggest just remove the condition Number of occurences of Name = 0. Let me know what happens.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  4. Hi... I tried this.But it is not worked.. the formatting condition also not worked.I tried to display the group name it is not display any thing

    ReplyDelete
  5. Hello spriya,

    this can be not working for some specific reasons. if you can send me more information on your problem, i would be able to help.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  6. Hi,
    I followed the same procedure.i am not able to get the login user and the group name. and when i set the sample data domain\Administrator, system give the error msg. so i put the Domain\username,then it acceptes. but the data not retrieved.so that the conditional formatting also not working.
    Thanks

    ReplyDelete
  7. Hello spriya,

    I will write down the points where you will need to have a closer look and recheck again.

    First, when you create a receiving data connection for the web service "UserGroup", did you give your SharePoint administrator username and password?

    Second, this is the most important part. You are exporting the form to extract it right, there I have mentioned we will be only focusing on GetGroupCollectionFromUser.xsd. But if you have exported the same form for many times, the .xsd file that we will need modify will change.

    You will get many .xsd files like GetGroupCollectionFromUser2.xsd, GetGroupCollectionFromUser3.xsd, GetGroupCollectionFromUser4.xsd like wise. So you should be extra careful to modify the correct .xsd file. So I am strongly advising you, first find this line "" and then do the modifications.

    First recheck above mentioned things and make sure there is no mistakes there. Then if you are still facing the problem, let me know. If you like we can have a team viewer session and fix this.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  8. Hello spriya,

    Did you paste your .xsd code when you reply? You cant do that. If you can send me a email to jaliya.udagedara@gmail.com.

    Please go through the steps again, you will find everything there.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  9. Hi,
    Thanks. I gone thro the steps.no where is mentioned the adminstrator username & password.
    As I said earlier, my xsd file is in xsd element form

    INSTEAD OF
    Regards,
    spriya

    ReplyDelete
  10. Hello spriya,

    Check receiving data connection for web service "UserGroup". Please don't put .xsd codes here. Because they will not be displayed.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  11. Hi,
    Thanks for reply.checked the dataconnection it isperfect.bt it not retrieved the data.the same thing happenedin other forms also.i used some other forms and workflows.in some forms the dataconnection retrieved the value from sharepoint library.in some forms it hs notretrieved.idnt knw why it is notworked in some forms.Pls help me in this regard.i folow the same procd in allforms.
    Thans & Regards,
    spriya

    ReplyDelete
  12. Hi Jaliya, can you help me in my issue
    in my case, i have one drop down filed, i have to disable when the user creating the form and submit to the SharePoint document library, this form is going to open by the different users, i have to enable this drop down user from the particular sharePoint group opens the form, for the rest of all others i have to disable when they open to see modify the form

    any help..

    ReplyDelete
  13. my company recently migrated to 2010 and my form (created in infopath 2010) is receiving errors when i try to use the GetUserProfileByName. It says "Operation Failure ---> Access Denied: To create a user profile, you must be an administrator, or create your own profile and have personal features rights.
    UnauthorizedAccessAccess Denied: To create a user profile, you must be an administrator, or create your own profile and have personal features rights."
    Is this something with the 2010 new server or do I have to custom code like you list above? We are not allowed to use SP designer.

    ReplyDelete
    Replies
    1. Hello,

      Of course, you should be an administrator.

      Delete
  14. Great ! I needed this and it works perfectly.
    It's a very detailed tutorial, thank you very much, your blog is very interesting.

    ReplyDelete
  15. Hi Jaliya,
    great nice tuto, but does it works with Sharepoint Online !

    I'm stuck on the GetGroupCollectionFromUser and the sample text, with my admin account, i get the followinb error :

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
    Impossible de trouver l'utilisateur.0x80131600

    ReplyDelete
  16. Hi jaliya,

    I finally had the answer from Microsoft about the loginname needed on the sample value of the GetGroupCollectionFromUser :

    the login name must be in the following form :
    i:0#.f|membership|user@domain.onmicrosoft.com

    I tried, it works !
    I just have to finish your tutorial !

    ReplyDelete
    Replies
    1. Your login is not working
      Yet I have applied the procedure,
      I do not understand

      Delete
  17. That's really nice mate. Good luck.

    ReplyDelete
  18. Thanks! That is a great article. I just mentioned it within our blog. And add an example for using it for better understanding.

    http://www.ilikesharepoint.de/2012/05/how-to-check-if-you-are-a-member-of-a-specific-group-within-an-infopath-form/

    ReplyDelete
    Replies
    1. Hi Dennis, just read your post. It's cool.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  19. hi thanks for your post. if current user present in multiple group. how to get all the group name. because by default it is return the first group name. can you please help me to fix it.

    ReplyDelete
    Replies
    1. Hi,

      GetGroupCollectionFromUser Method in the UserGroup web service will return all the groups the specific user belongs to.

      I am sorry, I couldn't test this, but in here you can drag and drop "Group" as a repeating table. It will give you a list of groups. "Name" is the Group name.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  20. Hi Jaliya,
    If current user present in multiple sharepoint group and how to get all the group name. because by default it is return the first group name. help me to fix these problem.

    ReplyDelete
    Replies
    1. Hi,

      GetGroupCollectionFromUser Method in the UserGroup web service will return all the groups the specific user belongs to.

      I am sorry, I couldn't test this, but in here you can drag and drop "Group" as a repeating table. It will give you a list of groups. "Name" is the Group name.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  21. Getting:

    There has been an error while processing the form.
    Click OK to resume filling out the form. You may want to check your form data for errors.


    Hide error details
    An error occurred while trying to connect to a Web service.


    Correlation ID:9ed917d2-41a7-459e-9df6-8085a7ee2de0


    any ideas>?

    ReplyDelete
  22. Its is working if Condition is like
    "Any Occurrences of Name" - is equal to - "custom account group"


    But it is not working is condition is like

    "Any Occurrences of Name" - is not equal to - "custom account group"

    In my scenario i want to hide fields if user not belongs to particular account group

    ReplyDelete
  23. 1.1. Create list "А" and one list item "BlaBla"
    1.2. Set required permission on item "BlaBla"
    2.1. In InfoPath create DataConnection on SharePoint List "A"
    2.2. Check, if count(list items from list "A")>0 then the user has permission!

    ReplyDelete
    Replies
    1. Thank you for this workaround, it is really helpful!

      Delete
  24. I get an error:

    You do not have permissions to access a Web service that provides data required for this form to function correctly.


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

    Group in question is set to allow everyone to see members.

    ReplyDelete
    Replies
    1. Hello Vojin Lekovic,

      i've got the same problem, since we changed to SSL. Using http protocol, every think worked fine. Did you find any solution for the problem.

      Best regards,
      Andi

      Delete
    2. Hi Vojin! Did you ever find a resolution to this problem? We are experiencing the same thing. Thanks!

      Delete
  25. First of all nice article but stuck in the mid i.e unable to create GetGroupCollectionFromUser everything works fine till the step "to provide sample for domain" after that when i click next prompt with the following error.
    User cannot be found.

    ReplyDelete
  26. Thank you.. very nice article

    Just one Clarification ,how to hide the field based on role instead of Group?

    ReplyDelete
  27. Fantastic - exactly what I needed! Thank you!

    ReplyDelete
  28. Hi All, Could any one help on my issue .

    I wast disable the section for all groups except one user group . I have followed above article ,section is disabling when user is not that group and when user is same in that group also disabling the section .

    ReplyDelete
  29. Thanks for this great article it really helped me out!
    I wrote an article following yours on how to get all of the current user's permission groups,
    I'll be happy if you check it out -
    http://www.sharepointbreak.com/2014/01/01/getting-current-users-sharepoint-permissions-groups-infopath-2010/
    Best Wishes
    Guy

    ReplyDelete
    Replies
    1. Thank you so much for the nice comment, definitely will be checking out your article.

      Delete
  30. Can we add/remove users from SharePoint Group using Infopath. If Yes, Please send me the steps or link.

    ReplyDelete
  31. Hi Jaliya, I did all the steps, I get the user name but the only issue is my field for the group return 0 while I am the group owner. I think that my web service is the fact. Why? Because when I give my url http://sever/_vti_bin/UserGroup.asmx, this error comes out:

    The SOAP response indicates that an error occurred on the server:
    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    Group cannot be found.0x80131600

    The other fact is I use the same url to get the user account name and it works: http://sever/_vti_bin/UserProfileService.asmx

    Any thoughts??

    ReplyDelete
  32. Beware that the UserProfileService requires authentication in SP2013. It does not in SP2010.

    ReplyDelete
  33. dear all,, jaliya, you got a really nice article. but I have some problems occurred when I follow the instruction you gave. when I already export the InfoPath file, the files I got are different with your as u describe. getgroupcollectionfromuser6.xsd and getgroupcollectionfromuser7.xsd not getgroupcollectionfromuser1.xsd. but I kept following your instruction to edit the file (that has size about 24kb). when I open the manifest file and choose design, theres no usergroup or user profile in the properties when I click advance view. could you please help me with this issue?
    thank you so much
    wiyono.bin.sudarno@gmail.com

    ReplyDelete
    Replies
    1. sorry i made mistakes.. i already get what it supposed to do.. thank you so much jaliyaudagedara...

      Delete
  34. Hi Jaliya iam getting an error like The parameter loginName cannot be empty or bigger than 251 characters please suggest

    ReplyDelete