Example #1
0
        private void AddOrRemoveUserToGroup(IEnumerable <string> groupIDs, UserOpType operationType, ClientContext clientContext)
        {
            if (groupIDs.Count() > 0)
            {
                string          userID    = txtUserID.Text;
                GroupCollection collGroup = clientContext.Web.SiteGroups;

                UserCreationInformation userCreationInfo = new UserCreationInformation();
                userCreationInfo.LoginName = userID;

                foreach (string groupID in groupIDs)
                {
                    Group oGroup = collGroup.GetById(Convert.ToInt32(groupID));
                    if (operationType.Equals(UserOpType.AddUser))
                    {
                        oGroup.Users.Add(userCreationInfo);
                        clientContext.ExecuteQuery();
                    }
                    else
                    {
                        User user = oGroup.Users.GetByLoginName(userID);
                        oGroup.Users.Remove(user);
                        clientContext.ExecuteQuery();
                    }
                } //  foreach (string group
            }     // if (groups.Count() > 0)
        }
Example #2
0
        private void AddOrRemoveUserToGroup(IEnumerable <string> groups, UserOpType operationType)
        {
            XPathNavigator root    = MainDataSource.CreateNavigator();
            string         siteUrl = root.SelectSingleNode("/my:EmployeeForm/my:siteUrl", NamespaceManager).Value;
            string         userID  = root.SelectSingleNode("/my:EmployeeForm/my:txtUserID", NamespaceManager).Value;

            if (groups.Count() > 0)
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        web.AllowUnsafeUpdates = true;
                        SPUser user = web.EnsureUser(userID);

                        foreach (string group in groups)
                        {
                            SPGroup spGroup = web.Groups[group];
                            if (spGroup != null)
                            {
                                if (operationType.Equals(UserOpType.AddUser))
                                {
                                    spGroup.AddUser(user);
                                }
                                else
                                {
                                    spGroup.RemoveUser(user);
                                }
                            }
                        } //  foreach (string group

                        web.AllowUnsafeUpdates = false;
                    }
                }
            } // if (groups.Count() > 0)
        }
        private void AddOrRemoveUserToGroup(IEnumerable<int> groupIDs, UserOpType operationType, ClientContext clientContext, string userID)
        {
            if (groupIDs.Count() > 0)
            {
                GroupCollection collGroup = clientContext.Web.SiteGroups;

                UserCreationInformation userCreationInfo = new UserCreationInformation();
                userCreationInfo.LoginName = userID;

                foreach (int groupID in groupIDs)
                {
                    Group oGroup = collGroup.GetById(groupID);
                    if (operationType.Equals(UserOpType.AddUser))
                    {
                        oGroup.Users.Add(userCreationInfo);
                        clientContext.ExecuteQuery();
                    }
                    else
                    {
                        User user = oGroup.Users.GetByLoginName(userID);
                        oGroup.Users.Remove(user);
                        clientContext.ExecuteQuery();
                    }
                } //  foreach (string group
            } // if (groups.Count() > 0)            
        }
        private void AddOrRemoveUserToGroup(IEnumerable<string> groups, UserOpType operationType)
        {
            XPathNavigator root = MainDataSource.CreateNavigator();
            string siteUrl = root.SelectSingleNode("/my:EmployeeForm/my:siteUrl", NamespaceManager).Value;
            string userID = root.SelectSingleNode("/my:EmployeeForm/my:txtUserID", NamespaceManager).Value;

            if (groups.Count() > 0)
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        web.AllowUnsafeUpdates = true;
                        SPUser user = web.EnsureUser(userID);

                        foreach (string group in groups)
                        {
                            SPGroup spGroup = web.Groups[group];
                            if (spGroup != null)
                            {
                                if (operationType.Equals(UserOpType.AddUser))
                                {
                                    spGroup.AddUser(user);
                                }
                                else
                                {
                                    spGroup.RemoveUser(user);
                                }
                            }
                        } //  foreach (string group
                       
                        web.AllowUnsafeUpdates = false;
                    }
                }
            } // if (groups.Count() > 0)            
        }