In this blog, we will see how to check if the current user is present in specific group or not, using SPServices.
Recently, I faced one situation where I needed to show li element for some specific group of users. For that, we have the below HTML - UL LI structure and from that, we need to show the "Create" link only for admin users (Group Name: Category Owner).
- <div>
- <ul>
- <li id=”Category1”>Category1</li>
- <li id=”Category2”>Category1</li>
- <li id=”Category3”>Category1</li>
- <li id=”Category4”>Category1</li>
- <li id=”Category5”>Category1</li>
- <li id=”Category6”>Category1</li>
- <li id=”Category7”>Category1</li>
- <li id=”create”>Create Category</li>
- </ul>
- </div>
The above structure is being created on the landing page of my site. Apart from that, I don’t want to show "Create Category" button to all the users. I have created one SharePoint group called “Category Owners” and added users into it.
In document.ready function, I have added the below code to load SPServices.
- <script>
- $(document).ready(function() {
- insureSPServices(InitializePage);
- });
- insureSPServices
- function is used to make sure SP Services is loaded
-
- function insureSPServices(callbackFunction) {
- if ($().SPServices == null) {
- jQuery.getScript("/_layouts/15/Project /Scripts/jquery.SPServices-0.7.2.js", callbackFunction);
- } else {
- callbackFunction.call(null, "Already Loaded");
- }
- }
-
- function InitializePage(data, textStatus) {
- isGroupMember("Category Owners ", function(result) {
- if (result) {
-
- document.getElementById("create").style.display = "block";
- }
- });
- }
-
- function isGroupMember(groupName, callback) {
- $().SPServices({
- operation: "GetGroupCollectionFromUser",
- userLoginName: $().SPServices.SPGetCurrentUser(),
- async: false,
- completefunc: function(xData, Status) {
- callback($(xData.responseXML).find("Group[Name='" + groupName + "']").length == 1);
- }
- });
- };
- </script>