Check if user exists before adding user to group - CSOM

Category: sharepoint 2010 customization

Question

Jiniv Thakkar on Wed, 18 Jan 2017 14:07:24


Hi All,

I have implemented the following script, but I am unable to check if user exists in group before adding the user in group in the following script.

Please help 

var user;
var visitorsGroup;

function AddUsers()
{
//Load Current Site
var clientContext = new SP.ClientContext();

//Get all groups in site
var groupCollection = clientContext.get_web().get_siteGroups();
     
// Get the group by name
visitorsGroup = groupCollection.getByName('Approvers');
     
//ensure SP User
var usr2 = clientContext.get_web().ensureUser('domain\\loginname');
     
//Get all SP Users in SP Group
var userCollection = visitorsGroup.get_users();
     
//Add User to Group
var oUSR2 = userCollection.addUser(usr2);

//Load data
clientContext.load(oUSR2); clientContext.load(userCollection); 
     
//Execute Query
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

        function onQuerySucceeded() {
            alert("Done");
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }


Thanks in Advance, Jiniv Thakkar


Replies

Lakshmanan Sethu on Wed, 18 Jan 2017 22:31:42


Hi,

Here you go

http://sharepointmantra.blogspot.com/2013/03/client-object-model-checking-if-user.html

http://www.c-sharpcorner.com/blogs/how-to-check-if-the-user-exists-in-a-particular-sharepoint-group-using-ecmascript1

Jiniv Thakkar on Fri, 27 Jan 2017 05:46:25


So I called this function to check if user exist

function IsCurrentUserMemberOfGroup(Uiduser)
{
             var grpName="Approvers";
             var userInGroup;
        var currentContext = new SP.ClientContext.get_current();
        var currentWeb = currentContext.get_web();

        var currentUser = Uiduser;
        //currentContext.load(currentUser);

        var allGroups = currentWeb.get_siteGroups();
        currentContext.load(allGroups);

        var group = allGroups.getByName(grpName);
        currentContext.load(group);

        var groupUsers = group.get_users();
        currentContext.load(groupUsers);

        currentContext.executeQueryAsync(OnSuccess, OnFailure);

        function OnSuccess(sender, args)
         {

            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) 
            {
                var groupUser = groupUserEnumerator.get_current();
                var logingrpuser = groupUser.get_loginName();
                logingrpuser  = logingrpuser.split("i:0#.w|")[1];
                if (logingrpuser == currentUser) 
                {
                    userInGroup = true;
                    break;
                }
            }
            if (userInGroup) 
            {
                     flag=1;   
                     console.log("New user has been added");      
            }

            else
             {
               flag=0;
                        
           	 }

        }

        function OnFailure(sender, args)
         {
		console.log("Error in adding user in group");
        }
    }

where Uiduser = domain\loginName example : Uiduser = contoso\jinivthakkar

depending on flag condition, I can decide whether to call Add user function.