SharePoint Online Check current user belongs to a particular SharePoint group using JavaScript object model

InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can check if the current logged in user belongs to a particular SharePoint group using JavaScript object model. Here I have a SharePoint group as “Team Site Owners” where I am checking is the current user belongs to the particular group or not.

Also you can see:
– Create Content Type using JavaScript object model in SharePoint Online Office 365

– How to get alternate languages from language settings in SharePoint online using Rest API?

– Add use custom action to list ECB menu in SharePoint Online using JavaScript object model

I have put the below JavaScript code inside a script editor web part.

<input id=”btnCheckUserCurrentGroup” onclick=”CheckCurrentUserBelongstoGroup()” type=”button” value=”Check current user belongs to group” />

<script src=”https://code.jquery.com/jquery-1.10.2.min.js” type=”text/javascript”></script>

<script type=”text/javascript”>

var userGroups;

function CheckCurrentUserBelongstoGroup()

{

var clientContext = new SP.ClientContext.get_current();

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

clientContext.load(this.currentUser);

userGroups = this.currentUser.get_groups();

clientContext.load(userGroups);

clientContext.executeQueryAsync(OnQuerySucceeded);

}

function OnQuerySucceeded() {

var isMember = false;

var groupsEnumerator = userGroups.getEnumerator();

while (groupsEnumerator.moveNext()) {

var group= groupsEnumerator.get_current();

if(group.get_title() == “Team Site Owners”) {

isMember = true;

break;

}

}

alert(isMember);

}

function OnQueryFailed() {

alert(‘Error occured’);

}

</script>

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply