예제 #1
0
        /// <summary>
        /// Adds a user to a group
        /// </summary>
        /// <param name="web">web to operate against</param>
        /// <param name="groupName">Name of the group</param>
        /// <param name="userLoginName">Loginname of the user</param>
        public static void AddUserToGroup(this Web web, string groupName, string userLoginName)
        {
            if (string.IsNullOrEmpty(groupName))
            {
                throw new ArgumentNullException("groupName");
            }

            if (string.IsNullOrEmpty(userLoginName))
            {
                throw new ArgumentNullException("userLoginName");
            }

            //Ensure the user is known
            UserCreationInformation userToAdd = new UserCreationInformation();

            userToAdd.LoginName = userLoginName;
            User user = web.EnsureUser(userToAdd.LoginName);

            web.Context.Load(user);
            //web.Context.ExecuteQueryRetry();

            //Add the user to the group
            var group = web.SiteGroups.GetByName(groupName);

            web.Context.Load(group);
            web.Context.ExecuteQueryRetry();
            if (group != null)
            {
                web.AddUserToGroup(group, user);
            }
        }
예제 #2
0
        /// <summary>
        /// Adds a user to a group
        /// </summary>
        /// <param name="web">web to operate against</param>
        /// <param name="groupName">Name of the group</param>
        /// <param name="userLoginName">Loginname of the user</param>
        public static void AddUserToGroup(this Web web, string groupName, string userLoginName)
        {
            //Ensure the user is known
            UserCreationInformation userToAdd = new UserCreationInformation();

            userToAdd.LoginName = userLoginName;
            User user = web.EnsureUser(userToAdd.LoginName);

            web.Context.Load(user);
            //web.Context.ExecuteQuery();

            //Add the user to the group
            var group = web.SiteGroups.GetByName(groupName);

            web.Context.Load(group);
            web.Context.ExecuteQuery();
            if (group != null)
            {
                web.AddUserToGroup(group, user);
            }
        }