magritton
11/29/2016 - 7:35 PM

This checks the user against a SharePoint group that then performs an action based on membership.

This checks the user against a SharePoint group that then performs an action based on membership.

var groupId = 9972; //group number - change*****

function isUserMemberOfGroup(userId, groupId, success,error) {

    var ctx = SP.ClientContext.get_current();
    var allGroups = ctx.get_web().get_siteGroups();
    var group = allGroups.getById(groupId);
    ctx.load(group, 'Users');
    ctx.executeQueryAsync(
        function (sender, args) {
            var userInGroup = findUserById(group.get_users(), userId);
            success(userInGroup);
        }, error);
    var findUserById = function (users, id) {
        var found = false;
        var e = group.get_users().getEnumerator();
        while (e.moveNext()) {
            var user = e.get_current();
            if (user.get_id() == id) {
                found = true;
                break;
            }
        }
        return found;
    };
}

function retrieveAllUsersInGroup(){
    var currentUserId = _spPageContextInfo.userId;
    isUserMemberOfGroup(currentUserId, groupId, 
      		function (isCurrentUserInGroup) {
      		    if (!isCurrentUserInGroup) {
      		        //disableApplicableControls();
      		        //hideApplicableControls();
      		        //console.log('Current user is not a member of Testgroup');
      		        var QASection = document.getElementById('QACompletes');
      		        QASection.style.display = 'none';
      		    }
      		    else
      		    {
      		    	var QASection = document.getElementById('QACompletes');
      		    	QASection.style.display = 'block';
      		        //console.log('Current user is a member of Testgroup');
      		    }
      		},
      		function(sender,args){
      		    alert('Check User - Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
      		});
}