Get group permission list in which current user is member using JavaScript in SharePoint Online CSOM

In blog, we will see how we can get group list in which current user is member using JavaScript.

Let's have example where we have few groups and we want to show current user is member of that group permissions or not.


Below is JavaScript code to fetch groups of current user. 



    1. var groups = ['Approve Users''Create Users''Review Users''Record Users''Initiate Users'];  
    2. var MemberOfGroup ="";  
    3. function setUserGroups(groupName) {  
    4.     if (groupName == "Approve Users") {  
    5.         MemberOfGroup += "Approve";  
    6.     } else if (groupName == "Create Users") {  
    7.         MemberOfGroup += "|Create";  
    8.     } else if (groupName == "Review Users") {  
    9.         MemberOfGroup += "|Review";  
    10.     } else if (groupName == "Record Users") {  
    11.         MemberOfGroup += "|Record";  
    12.     } else if (groupName == "Initiate Users") {  
    13.         MemberOfGroup += "|Initiate ";  
    14.     }  
    15. }  
    16. // check member of multiple group.  
    17. function IsCurrentUserMemberOfGroups() {   
    18.     var dfd = $.Deferred(function () {  
    19.             var currentContext = new SP.ClientContext.get_current();  
    20.             var currentWeb = currentContext.get_web();  
    21.             var currentUser = currentContext.get_web().get_currentUser();  
    22.             currentContext.load(currentUser);  
    23.             var allGroups = currentUser.get_groups();  
    24.             clientContext.load(allGroups);  
    25.             currentContext.executeQueryAsync(  
    26.                 function () {  
    27.                     var groupsEnumerator = allGroups.getEnumerator();  
    28.                     while (groupsEnumerator.moveNext()) {  
    29.                         var group = groupsEnumerator.get_current();  
    30.                         if (groups.indexOf(group.get_title()) > -1) {  
    31.                             setUserGroups(group.get_title());  
    32.                         }  
    33.                     }  
    34.                     window.location.href = window.location.href + "&MemberOfGroup=" + MemberOfGroup;  
    35.                     dfd.resolve();  
    36.                 },  
    37.                 function () {  
    38.                     dfd.reject(args.get_message());  
    39.                 }  
    40.             );  
    41.     });  
    42.     return dfd.promise();  
    43. }  

Comments

Popular Posts

Contact Application Using ASP.NET Core Web API, Angular 6.0, And Visual Studio Code - Part One

Contact Application Using ASP.NET Core Web API, Angular 6.0, And Visual Studio Code - Part Two

Send Email With SharePoint Lookup Columns Data Using Power Automate

Contact application - Upgrade Asp.net Core 2.0 to 2.1

Create SSRS Drill Down Report

Typescript Basics

Chat Application using Angular 8, Asp.net Core 2.2.0, Signal R 1.1.0