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.
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.
- var groups = ['Approve Users', 'Create Users', 'Review Users', 'Record Users', 'Initiate Users'];
- var MemberOfGroup ="";
- function setUserGroups(groupName) {
- if (groupName == "Approve Users") {
- MemberOfGroup += "Approve";
- } else if (groupName == "Create Users") {
- MemberOfGroup += "|Create";
- } else if (groupName == "Review Users") {
- MemberOfGroup += "|Review";
- } else if (groupName == "Record Users") {
- MemberOfGroup += "|Record";
- } else if (groupName == "Initiate Users") {
- MemberOfGroup += "|Initiate ";
- }
- }
- // check member of multiple group.
- function IsCurrentUserMemberOfGroups() {
- var dfd = $.Deferred(function () {
- var currentContext = new SP.ClientContext.get_current();
- var currentWeb = currentContext.get_web();
- var currentUser = currentContext.get_web().get_currentUser();
- currentContext.load(currentUser);
- var allGroups = currentUser.get_groups();
- clientContext.load(allGroups);
- currentContext.executeQueryAsync(
- function () {
- var groupsEnumerator = allGroups.getEnumerator();
- while (groupsEnumerator.moveNext()) {
- var group = groupsEnumerator.get_current();
- if (groups.indexOf(group.get_title()) > -1) {
- setUserGroups(group.get_title());
- }
- }
- window.location.href = window.location.href + "&MemberOfGroup=" + MemberOfGroup;
- dfd.resolve();
- },
- function () {
- dfd.reject(args.get_message());
- }
- );
- });
- return dfd.promise();
- }
Comments
Post a Comment