SharePoint 2013 - SPLessons

Check if current user is in a specified group Using SPServices

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Check if current user is in a specified group Using SPServices

Check if current user is in a specified group Using SPServices

Description:  Hi every one, today we are going to Check if current user is in a specified group Using SPServices (Client Object Model), for example  in real-time scenario suppose if the logged user does not belongs to a Group then we have hide some content in a SharePoint site . So we have restrict the user, for that i created one group, added a user into that group.

What is SPService? :
SPServices is a jQuery library which abstracts SharePoint's Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install. We can say SPService is independent of SharePoint version. Write once execute any SharePoint environment. It will reduce the lot burden on the server by running the business logic on the client browser rather than in SharePoint Server.

 

Step1 :
Create one group and add one or more users into a group.

Step2 :
We can use below mention code in where ever to restrict the current user like application form, web-part  or  Content editor Webpart. [javascript] <script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”></script> <script type=”text/javascript” src=”http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js”></script> <script type=”text/javascript”> $(document).ready(function () { $().SPServices({ operation: “GetGroupCollectionFromUser”, userLoginName: $().SPServices.SPGetCurrentUser(), async: false, completefunc: function (xData, Status) { if ($(xData.responseXML).find(“Group[Name='Admingroup']“).length == 1) { alert(“login inside of the Group user”); } else { alert(“login outside of the Group user”); } } }); /*close().SPServices({ */ }); /* close (document).ready(function() { */ </script> [/javascript]

Step3 :
We can see out put like below If the user belongs to a group.   If the user does not belongs to a group.   Like this we can check current user belongs  into  SharePoint site, list, group...etc