5

One of the fields on list is User Picker where often are place AD groups.

Authentication is set up with claims.

While presenting the item, users which are in this AD groups (and others mentioned in this field), need to see an button. Other (which have permission to see this item but were not mentioned) should not.

How can I get information, whether user is in this AD group?

1
  • Did you find any solution for this? Feb 6, 2019 at 10:09

3 Answers 3

1

I use an extension method, like this:

 public static bool InGroup(this SPUser User, string GroupName)
    {
        return User.Groups.Cast<SPGroup>().Any(g => g.Name.ToLower() == GroupName.ToLower());
    }

Then call it like this:

bool inGroup = spuser.InGroup("GroupName");
2
  • This are not SPGroup, but AD Groups. Additional difficulty that they are claim based and SharePoint sees them as users.
    – Jacek
    Aug 17, 2016 at 9:21
  • How can AD user come in claim based? Can you be more specific like you want to check this for the claim based users or for the AD users ? Because if your User is in AD group, you still will need to add him in any sharepoint group to give him access in sharepoint, untill and unless he don't have access. He will not be able to access SharePoint. Nd for the other case i.r claim based, the data for that is maintained in SQL database . So for which user you want to check ?
    – Muskan
    Aug 17, 2016 at 9:25
0

You can get a flat transitive/recursive list of the user's AD security and O365 group memberships using this endpoint:

/_api/SP.Publishing.SitePageService.GetCurrentUserMemberships
-1

Steps to find User belongs to AD Group.

  • Create a SharePoint Group and add AD Group into SharePoint Group Use the below code to find user belongs to SharePoint Group or not

How to check if current user belongs to SharePoint group via CSOM (JavaScript):

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

    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(groupName);
    currentContext.load(group);

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

    currentContext.executeQueryAsync(OnSuccess,OnFailure);

    function OnSuccess(sender, args) {
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }  
        OnComplete(userInGroup);
    }

    function OnFailure(sender, args) {
        OnComplete(false);
    }    

}

Usage:

function IsCurrentUserHasContribPerms() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        // The current user is in the [Members] group!
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');

Check if user is in a specified group

5
  • AD groups are not handled by this Jan 19, 2018 at 10:16
  • I didn't say this is only the way to find user belongs to AD group.. by using client context this would be the simplest way. Jan 22, 2018 at 0:33
  • @NagarajuPattem this does not work with AD groups, it doesn't appear in the list of groups of the user.
    – netadictos
    Oct 11, 2018 at 7:57
  • @netadictos If you follow all the above steps you can identify AD Groups which we implemented long back and let me know where you have the issue. Nov 1, 2018 at 5:13
  • Nagaraju - can you provide any update for AD group scenario. Otherwise you can simply put the other link reference in comment Feb 6, 2019 at 10:11

Your Answer

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

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