1

I use the following code in javascript to display name of logged in user.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUserData);

Is there a code that will also tell me security group of currently logged in user? I mean the code that will tell me whether user belongs to "Members" group or "Approvers" group etc.

3 Answers 3

4

If you need the Group Name, unfortunately there is no direct way to do that. But we can get the current user and get user collection for one group. Then you can check the user collection from one group to see whether it contains the current user.

https://stackoverflow.com/questions/6428300/get-current-users-group-using-the-sp-2010-javascript-client-side-object-model

1
  • While the link does answer this question, it's better to add the relevant information from the link to your answer in case anything happens to the SO question you've linked to. Commented Dec 16, 2013 at 3:09
4

if you are working on SharePoint 2013 Version, then it is easy to get current user groupcollection through rest api in javascript.

script would be like this :

 function getCurrentUserGroupColl(UserID)
 {
  $.ajax
  ({
  url: "/sites/dev/_api/web/GetUserById("+UserID+")/Groups",
  type: "GET",
  headers: { "Accept": "application/json; odata=verbose","X-RequestDigest": $("#__REQUESTDIGEST").val() },
  dataType: "json",
  async: true,
   success: function(data){
      /* get all group's title of current user. */
      var results = data.d.results;
      for(var i=0; i< results.length; i++)
      {
            console.log(results[i].Title)
      }
  }
  });
}


function getCurrentUser()
{
 $.ajax
  ({
  url: "/sites/dev/_api/web/CurrentUser",
  type: "GET",
  headers: { "Accept": "application/json; odata=verbose","X-RequestDigest": $("#__REQUESTDIGEST").val() },
  dataType: "json",
  async: true,
  success: function(data){
    getCurrentUserGroupColl(data.d.Id)
  }
  });
}  

$(document).ready(function(){ getCurrentUser();  });

Hope it helps!!

1
  • I think there is a bug in your JavaScript - in function getCurrentUserGroupColl - your for loop has a check i<results.length; but for me results is undefined. I think you want i<data.d.results.length; Commented Aug 10, 2015 at 20:04
0

You can try this code snippet to target your functionality through ECMA script:

var adminUsers;
var vCurrUserName;
var groupCollection;
var _group;

function checkUser() {
var currentContext = new SP.ClientContext.get_current();

//get current user

vCurrUserName = currentContext.get_web().get_currentUser();

currentContext.load(vCurrUserName);

// get Admin group

groupCollection = currentContext.get_web().get_siteGroups();
currentContext.load(groupCollection);

//var _group = groupCollection.getById(10); // ID of the Group

currentContext.executeQueryAsync(Function.createDelegate(this, this.checkUserSuccess),
  Function.createDelegate(this, this.checkUserFailure));

}
var vIsAdmin = false;
function checkUserSuccess() {
var currentContext = new SP.ClientContext.get_current();

var grpEnum = groupCollection.getEnumerator();

while(grpEnum.moveNext()){
var grpItem = grpEnum.get_current();
currentContext.load(grpItem);

// here you can add one more if condition to check for "Approvers" group.

if(grpItem.get_title() == "Member"){
    _group = grpItem;
    break;
 }
}

adminUsers = _group.get_users();
currentContext.load(adminUsers);

currentContext.executeQueryAsync(Function.createDelegate(this, this.findUserInGroup), 
Function.createDelegate(this, this.checkUserFailure));
}

function findUserInGroup(){
var listEnumerator = adminUsers.getEnumerator();

while (listEnumerator.moveNext()) {

var item = listEnumerator.get_current();

//check current user login name and user login name from admin group

if (vCurrUserName.get_loginName() == item.get_loginName()) {

vIsAdmin = true;

break;

    }

}

if (vIsAdmin) {
  // do something here
}
else
{
  // do something here
}

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.