Monday, November 21, 2011

Sharepoint 2010 - Get Current User's sharepoint group information using javascript.

Goal

To retrieve the SharePoint Group information of Current User logged in using Javascript.

Solution

  1. Dowload the "jquery.SPServices-0.6.2.js" file from CodePlex. Save the file somewhere. I save it in "Style Library".
  2. Use the below given script to get the list of SharePoint Group using javascript.
<!--http://www.jquery.com-->
<script>    !window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<!--http://spservices.codeplex.com/releases/view/64390-->
<script language="javascript" tye="text/javascript" src="/style library/jquery.SPServices-0.6.2.js"></script>
<!--Custom Script-->
<script language="javascript" tye="text/javascript">
    $(document).ready(function () {

        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {
                //Shows the XML returned from the server
                alert(xData.responseXML.xml);
                //Replace the "<<SHAREPOINT GROUP NAME>>" with your group name will tell that user is on a particular group or not.
                if ($(xData.responseXML).find("Group[Name='<<SHAREPOINT GROUP NAME>>']").length == 1) {
                    alert('User in a group');
                }
            }

        });

    });
</script>
This should be it. Happy Coding.

10 comments:

  1. Thanks its Working.........

    ReplyDelete
  2. Hello how can i add users who click on a link in sharepoint to a sharepoint group?

    ReplyDelete
    Replies
    1. @Anonymous - I think you will have to write a piece of code for add the user to SharePoint Group, this might help you. http://sarangasl.blogspot.in/2009/12/add-user-to-sharepoint-user-group.html

      Delete
  3. Gr8... It is working ... Thanks

    ReplyDelete
  4. just be aware that $().SPServices.SPGetCurrentUser() is itself making an ajax call to fetch the username...

    ReplyDelete
  5. its not working in Firefox.
    is there any other solution which will work on all browser??

    ReplyDelete
    Replies
    1. Not possible the solution produced is tested in all the browsers. Its being used in a live system as well.

      Although there is a very big possibility of version.

      Thanks,
      Maulik Dhorajia

      Delete
  6. Im not able to find group name in my xml . Kindly advice...Xml is below

    "

    "

    ReplyDelete