0

I have this method to check whether the current logged in user belongs to a certain group (IT Department) or no? It returns true if the user is in the group. However I am member of that group but I get a false. What could possibly be problematic ?

function isCurrentUserMemberOfGroup(groupName) {
var isUserInGroup = false;
$.ajax({
    async: false,
    headers: { "accept": "application/json; odata=verbose" },
    method: "GET",
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups",
    success: function (data) {
        data.d.results.forEach( function (value) {
            if (value.Title == groupName) {               
                 isUserInGroup = true;
            }
        });
    },
    error: function (response) {
        console.log(response.status);
    },
});    
return isUserInGroup;
}

var isAdmin = isCurrentUserMemberOfGroup("IT Department");
 console.log('isadmin?' + isAdmin)
5
  • On which environment you are running this code? Is it SharePoint online or on Premise? If SharePoint online then on modern Page or classic page? Commented Sep 12, 2019 at 11:23
  • Sharepoint Online. Modern
    – Y_Lakdime
    Commented Sep 12, 2019 at 11:26
  • are you able to get the URL in your ajax call? Commented Sep 12, 2019 at 11:31
  • @user3660293: Do you want to use this code in SPFx web part or you would like to use it in classic experience page? Commented Sep 12, 2019 at 12:23
  • i have it in Modern Script Editor web part
    – Y_Lakdime
    Commented Sep 12, 2019 at 12:24

3 Answers 3

1

Are you able to access the _spPageContextInfo.webAbsoluteUrl ?

As per this post _spPageContextInfo does not work in the Modern site of SharePoint Online.

You can use the approach mentioned in the mentioned link to get the site URL.

3
  • how do I know if I am able to access that page?
    – Y_Lakdime
    Commented Sep 12, 2019 at 11:41
  • Write that in console of the browser and check if you are getting the URL or not ? Commented Sep 12, 2019 at 12:12
  • it works. It give me alot of data
    – Y_Lakdime
    Commented Sep 12, 2019 at 12:15
0

can you please change the actual rest query to get the value of all the groups current user is associated to as below in bold text

 function isCurrentUserMemberOfGroup(groupName) {
    var isUserInGroup = false;
    $.ajax({
        async: false,
        headers: { "accept": "application/json; odata=verbose" },
        method: "GET",
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/?$expand=groups",
        success: function (data) {

success: function (data) { 
for (var i = 0; i < data.d.Groups.results.length; i++) 
     { 
   console.log(data.d.Groups.results[i].Title); 
    if (data.d.Groups.results[i].Title == groupName) {               
                     isUserInGroup = true;
                }
       }

        },
        error: function (response) {
            console.log(response.status);
        },
    });    
    return isUserInGroup;
    }

Mind the change in:

url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/?$expand=groups",
12
  • i get : TypeError: Cannot read property 'forEach' of undefined
    – Y_Lakdime
    Commented Sep 12, 2019 at 11:29
  • instead of data.d.results use data.d.Groups.results Commented Sep 12, 2019 at 11:44
  • let me know if you still receive the error or any other error Commented Sep 12, 2019 at 11:53
  • i get RangeError: Maximum call stack size exceeded
    – Y_Lakdime
    Commented Sep 12, 2019 at 12:05
  • so you use different code to make it work. the above answer provided by dhuvil is just whether you can access _spPageContextInfo.webAbsoluteUrl is in page or not. there is no solution but you mention as Answer. may i know the reason Commented Sep 12, 2019 at 12:31
0

SharePoint Online - check if a User is in a group with jSOM for your reference:

https://www.c-sharpcorner.com/blogs/sharepoint-online-check-if-a-user-is-in-a-group-with-jsom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.