Monday, 1 August 2016

Get logged in user's group details in SharePoint 2010

Hi,

I need to show/hide content based on logged in user's group, for this i need to get logged in user's group details.

There are two way to do this,

1. manually checking user belongs to a group or not, if user belongs to group then show otherwise no
2. get all loggedin user's groups then based on group name we will how content.

Option 1 is very lengthy and its not generic.
Option 2 is very generic and we dont need to worry if new groups added in future.

So, am going to use option 2.

Solution:

1. download the "jquery.SPServices-0.6.2.js" file from codeplex.
2. Use the below code.

<script type="text/javascript" src="../SiteAssets/jquery.min.js"></script>
<script>    !window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<script language="javascript" tye="text/javascript" src="../SiteAssets/jquery.SPServices-0.6.2.js"></script>
<script language="javascript" tye="text/javascript">
$(document).ready(function () {
var loggedinUserGroup="";
        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {
                //Shows the XML returned from the server
$(xData.responseXML).find("Group").each(function()
{
 //show and hide based on group name ..
});
}

});
});
</script>


No comments:

Post a Comment