2

I have wrote following code to add users to SharePoint group. It works fine and add user to the group first time. But if I remove the user from site manually and re run the app there is no exception from the application, but user is not adding to the group. I have debugged and checked, app passes user adding part also. What may be the problem? Any guess please?

 static void Main(string[] args)
        {
            Console.WriteLine("Enter site collection URL:"); // Prompt
            string URL = Console.ReadLine();
            Console.WriteLine("Enter Login Name of the user:"); // Prompt
            string loginName = Console.ReadLine();
            Console.WriteLine("Enter Group Name of the user you want to add:"); // Prompt
            string groupName = Console.ReadLine();
            AddUserToAGroup(loginName, groupName, URL);
        }
        private static void AddUserToAGroup(string userLoginName, string userGroupName, string url)
        {
            //Executes this method with Full Control rights even if the user does not otherwise have Full Control
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                //Don't use context to create the spSite object since it won't create the object with elevated privileges but with the privileges of the user who execute the this code, which may casues an exception
                using (SPSite spSite = new SPSite(url))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        try
                        {
                            //Allow updating of some sharepoint lists, (here spUsers, spGroups etc...)
                            spWeb.AllowUnsafeUpdates = true;

                            SPUser spUser = spWeb.EnsureUser(userLoginName);

                            if (spUser != null)
                            {
                                SPGroup spGroup = spWeb.SiteGroups[userGroupName];

                                if (spGroup != null)
                                {
                                    spGroup.AddUser(spUser);
                                    spGroup.Update();
                                    spWeb.Update();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            //Error handling logic should go here
                        }
                        finally
                        {
                            spWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }

            });
        }

2 Answers 2

1
public override void ItemAdded(SPItemEventProperties properties)
{      
base.ItemAdded(properties);
string title = properties.List.Title;

if (title == "List Name")
{
    SPListItem item = properties.ListItem;
    try
     {
        SPSecurity.RunWithElevatedPrivileges(delegate()
         {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
             {
               using (SPWeb spWeb = properties.OpenWeb())
                {
                spWeb.AllowUnsafeUpdates = true;
                var name = item["user group field name"].ToString();
                string[] result = name.Split('#');
                SPUser spUser = spWeb.EnsureUser(result[1]);



 if (spUser != null)
              {
                SPGroup spGroup = spWeb.Groups["list name to which item to add"];

            if (spGroup != null)
            spGroup.AddUser(spUser);
         }
      }
    }
});
}
catch (Exception ex)
{

}
finally
{
    //spWeb.AllowUnsafeUpdates = false;
}

   }


}
1

May be after manual removal user from group you still open the page and after your code finished you refresh spgroup page by clicking F5 ? Try to click Enter on address bar instead F5. Because F5 will repeat your last operation on a page(removal user).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.