0

I have a SharePoint Add-on for Office 365. It is currently a SharePoint Hosted app and I would like to keep it that way.

My Problem is I am trying to find out if a user is a member of a group either directly or through an AD group. I'm currently querying the user, using the API, for a list of groups they are a member of but this only returns groups that the have been added directly to.

I have seen the workaround on Check users group membership via REST API using AD-Groups in SP-Groups but I don't have control over the SharePoint groups

Do anyone know if it is possible to check if a user is a member of a SharePoint group when they are added via an AD group using the REST API or JSOM?

1

1 Answer 1

0

Try this to check the current user group

    <script src="https://c4968397007.dc07.loc:2000/sites/DeveloperSite/Style%20Library/jquery-1.9.0.min.js"></script>

 <script type="text/javascript">

 function TempMethod()

 {

 //alert('Loaded');

 }

 function CheckCurrentUserMembership() {

 var clientContext = SP.ClientContext.get_current();

 this.currentUser = clientContext.get_web().get_currentUser();

 clientContext.load(this.currentUser);

 this.userGroups = this.currentUser.get_groups();

 clientContext.load(this.userGroups);

 clientContext.executeQueryAsync(OnQuerySucceeded);

 }

 function OnQuerySucceeded() {

 var isMember = false;

 var groupsEnumerator = this.userGroups.getEnumerator();

 while (groupsEnumerator.moveNext()) {

 var group= groupsEnumerator.get_current();

 if(group.get_title() == "Administrator Group") {

 isMember = true;

 alert('Success');

 break;

 }

 }

 OnResult(isMember);

 }

 function OnQueryFailed() {

 OnResult(false);

 }

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

 ExecuteOrDelayUntilScriptLoaded(TempMethod, "SP.js");

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

 //ExecuteOrDelayUntilScriptLoaded(TempMethod, "SP.ClientContext");

 CheckCurrentUserMembership();

 });

 </script>

I Hope it will help you, Change it to as per you requirement.

1
  • Thanks for this, unfortunately, this return the same as the REST API and only get the groups the user is a direct member of. May 30, 2017 at 9:59

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.