1

this is the current script that i found online which checks if a user logged into SharePoint is part of a group. As you can see in the syntax below, it is used to check if the user is part of the devHelpDeskV2 Members group

function IsCurrentUserMemberOfGroup() {
    var grpName = "devHelpDeskV2 Members";
    var userInGroup;
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();

My question is, how should i modify or add so that i can check on more groups. For example, Let's say i have 2 more groups, devAccount and devShipping. Will it work if i add them like so?

Just as an update, the following is the code that i currently am using on SPO edit form page via Script Editor. The goal was to check if the user was part of a group and disable some fields in edit form so that the user will not be able to change their entry. The part which does the disabling of the fields is the if (userInGroup) part

How should i fit that into the coding that you guys suggest?

$(document).ready(function(e) {

    ExecuteOrDelayUntilScriptLoaded(IsCurrentUserMemberOfGroup, "sp.js");
});

function IsCurrentUserMemberOfGroup() {
    var grpName = "devHelpDeskV2 Members";
    var userInGroup;
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();

    var currentUser = currentContext.get_web().get_currentUser();
    currentContext.load(currentUser);

    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);

    var group = allGroups.getByName(grpName);
    currentContext.load(group);

    var groupUsers = group.get_users();
    currentContext.load(groupUsers);

    currentContext.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args) {

        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }
        if (userInGroup) {
            //alert("user exists in the group");
            //do some operation.
            $("input[type=text],textarea").prop("disabled","true").css('background-color','#F6F6F6');

        } else {
            //alert("user doestn't exist in the group");
        }

    }

    function OnFailure(sender, args) {
        //error message.
    }
}
2

We can use the REST API below to get all the current user groups, and then check if groups in the "grpName".

/_api/web/currentuser/groups?$select=Title

The following code for your reference.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function(){
    $(".sp-peoplepicker-delImage").hide();
    if(IsCurrentUserMemberOfGroup()){
        //do some operation.
        $("input[type=text],textarea").prop("disabled","true").css('background-color','#F6F6F6');
    }
});
function IsCurrentUserMemberOfGroup(){
    var grpName=["devHelpDeskV2 Members","devAccount","devShipping"];
    var isUserInGroups=false;
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups?$select=Title";
    $.ajax({
        url: url,
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            for(var i=0;i<grpName.length;i++){
                for(var j=0;j<data.d.results.length;j++){
                    if(grpName[i]==data.d.results[j].Title){
                        isUserInGroups=true;
                    }
                }
            }                       
        },
        error: function (error) {
            //console.log(JSON.stringify(error));  
        }
    });
    return isUserInGroups;
}
</script>
  • Thanks LZ, i have updated my question with more information. Need help in placing the IF statement into your code – Tommy0824 Sep 16 '18 at 22:44
  • I update my code in the reply, add the if statement in the code. – LZ_MSFT Sep 17 '18 at 1:47
  • hope you don't mind asking, i have a code with removed the X from a people picker field. The code that i used is: $(".sp-peoplepicker-delImage").hide(); Doesn't seem to be working anymore. Everything else does. Any ideas? Thanks – Tommy0824 Sep 17 '18 at 2:12
  • Add the code into $(function(){ //add code });, if you want to hide the removed code, we an also use CSS to achieve it. <style type="text/css">.sp-peoplepicker-delImage{display:none;}</style> – LZ_MSFT Sep 17 '18 at 2:22
  • Hi LZ, i tried adding the code after $(function(){ like your updated example above, didn't seem to work. Where would i add the CSS code? Thanks – Tommy0824 Sep 17 '18 at 2:55
1

may be this code will be helpful.

var ConstantGroupName=["Test1"];//decalre groups where you want to check user
$.getJSON(_spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/?$expand=groups&$select=groups/Title").done(function(d){
console.log(d.Groups);
if(d)
{
    if(d.Groups.length>0)
    {

        var groupsName=$.map(d.Groups,function(o){return o.Title});//this will retrun current user group title array;
        var found = false;
        for (var i = 0; i < ConstantGroupName.length; i++) 
        {
            if (groupsName.indexOf(ConstantGroupName[i]) > -1)
                {
                    found = true;
                    break;
                }
        }
        console.log(found);
       }
    }
})
0

You can use the below script to check current logged in users is a group.

function checkUserInGroup(groupArray, grpArray) {
 return groupArray.some(function (v) { return grpArray.indexOf(v) != -1; 
 });
}

function getCurrentUserGroup() {   
return jQuery.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/?$expand=groups&$select=groups/Title",
    method: "GET",
    async: false,
    headers: { "Accept": "application/json; odata=verbose" }        
});    
}

function successgetUsers(data) {
var grpArray = ["group1", "group2", "group3", "group4"];

var groupArray = new Array();
jQuery.each(data.d.Groups.results, function (i, result) {
    groupArray.push(result.Title);
});

checkUserInGroup(groupArray, grpArray) ? alert('found user in group') : 
 alert('Not found user in group');  
}

function failgetUsers(){
alert('failed to getCurrentUserGroup ');
}

getCurrentUserGroup().then(successgetUsers, failgetUsers);
  • Thanks Robert, i have updated my question with more details. I need help / advise in making the code work – Tommy0824 Sep 16 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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