In this blog you will see how to check if the user exists in a particular SharePoint group using ECMAScript.
- <script type="text/javascript">
- ExecuteOrDelayUntilScriptLoaded(IsUserExists, "sp.js");
-
- var group;
- var users;
- var ctx;
- var groupCollection;
- var user;
- var currentuser;
-
- function IsUserExists() {
- ctx = SP.ClientContext.get_current();
- groupCollection = ctx.get_web().get_siteGroups();
- currentuser=ctx.get_web().get_currentUser();
- group = groupCollection.getById(4103);
- ctx.load(group);
- ctx.load(currentuser);
- ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetGroupSuccess), Function.createDelegate(this, OnFailure));
- }
-
- function OnGetGroupSuccess() {
- users=group.get_users();
- ctx.load(users);
- ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetuserSuccess), Function.createDelegate(this, OnFailure));
- }
-
- function OnGetuserSuccess() {
- var userEnumerator = users.getEnumerator();
- while (userEnumerator.moveNext()) {
- var user = userEnumerator.get_current();
- if (user.get_id() == currentuser.get_id()) {
- alert("User Exists")
- break;
- }
- else
- {
- }
- }
- }
-
- function OnFailure(sender, args) {
- alert("Failed to execute IsCurrentUserMemberOfGroup method");
- }
- </script>