3

I have a security group ADGroup in AD which has Member1 as its member, now i created a SharePoint group named SPGroup and added AD group ADGroup in it.

If i check permission for Member1 then i see permissions given through SPGroup.

But if i am using REST API

_spPageContextInfo.webAbsoluteUrl+"/api/web/getuserbyid(10)/groups"

then i dont get anything, a empty array is returned.

How can i get SPGroup as result?

2

Instead of this please use the below service URL

_api/web/siteusers/getbyid(48)/groups

Loop through this and get the Desired group names.

Hope This Helps!!

  • this api works only if users are directly added to Sharepoint group, if AD group is added to SP group then this request gives empty array of groups. – user2889674 May 12 '16 at 8:09
0

The following example demonstrates how to determine whether current users belongs to the specified groups:

getCurrentUserWithDetails()
.done(function(data)
{
    var groupNames = ['News Owners','Approvers'];
    //determine wether current user is a memeber of group(s) 
    var userGroups = data.d.Groups.results;
    var foundGroups = userGroups.filter(function(g){ return groupNames.indexOf(g.LoginName) > -1});

})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

where getCurrentUserWithDetails function is used to get current user with groups:

function getCurrentUserWithDetails()
{
    var endpointUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';
    return $.ajax({
            url: endpointUrl,
            method: "GET",
            contentType: "application/json;odata=verbose",
            headers: {   
             "Accept": "application/json;odata=verbose"
            } 
        });
}

Key points: more optimal from performance perspective since only a single request is submitted to the server

Updated :-

Please find one more approach to get your desired output in below mentioned link.

SharePoint 2013: Get current user group collection through Rest API

  • this works only if the user is added to sharepoint group directly, but if the user is added to a AD security group then that security group is added to sharepoint group then it doesnt work. the result from rest api returns empty object in data.d.Groups.results. – user2889674 May 11 '16 at 14:05
  • Please check here if it helps you.....stackoverflow.com/questions/33164019/… – Hardik May 11 '16 at 14:11
0

i have achieved the required result using the following code.

(function () {
angular.module('app').factory('masterdatafactory', ['$http', function ($http) {

    var datafactory =
        {
           getUserDetails:getUserDetails
        }
       return datafactory;

function getUserDetails(groupName)
    {
        var userID=_spPageContextInfo.userId;
        return $http({
            method: "GET",

            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('"+groupName+"')/CanCurrentUserViewMembership",
            headers: {
                "accept": "application/json; odata=verbose"
            }

        }).success(function (data) {
            return data; 
        }).error(function () {
            alert("error");
        })

    }

then in controller

(function () {
angular.module("app").controller('metadatareportcontroller', ['$scope', 'masterdatafactory', '$filter', '$timeout','$q','$sce','uiGridExporterConstants','uiGridExporterService', function ($scope, masterdatafactory, $filter, $timeout,$q,$sce,uiGridExporterConstants,uiGridExporterService) 
{

     masterdatafactory.getUserDetails('SPGroup').then(function(data){
      var result=data.data.d.CanCurrentUserViewMembership;
})

// Group members(not everyone) should be able to view group membership in group setting. // if you need to check membership for large no. of groups then this request have to be made multiple times, which could be a problem in terms of performanc(may be didn't checked)

0

In SharePoint If you add AD group then SharePoint will consider AD group as a single user instead of group. So you will not able to get user from group after adding in SharePoint groups. However you can try this with code

bool reachedMaxCount;
SPWeb web = SPContext.Current.Web;
int limit = 100;
string group = "Domain\\SecurityGroup";
SPPrincipalInfo[] users = SPUtility.GetPrincipalsInGroup(web, group, limit, out reachedMaxCount);

Reference

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.