Madhawa Learns To Blog : C#, Java

.net, c#, java,sql, OOAD and more mad memory dumps...

  Home  |   Contact  |   Syndication    |   Login
  47 Posts | 0 Stories | 57 Comments | 15 Trackbacks

News

Archives

Post Categories

Blog Roll

Hang Outs

My old blog

        /// <summary>
        /// Add a user to a Sharepoint group
        /// </summary>
        /// <param name="userLoginName">Login name of the user to add</param>
        /// <param name="userGroupName">Group name to add</param>
        private void AddUserToAGroup(string userLoginName, string userGroupName)
        {
            //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(Page.Request.Url.ToString()))
                {
                    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.Groups[userGroupName];

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

            });
        }

 

Here in this method you have to set "spWeb.AllowUnsafeUpdates = true" to allow updating some sharepoint lists.

posted on Tuesday, November 18, 2008 6:47 AM

Feedback

# re: Add a user programmatically to a User Group in SharePoint 3/3/2010 12:04 PM Bhuvan
Thanks for the post

# re: Add a user programmatically to a User Group in SharePoint 9/16/2010 2:16 PM Joe Mamma
Thank you for this (:

# re: Add a user programmatically to a User Group in SharePoint 12/10/2010 4:16 AM karthik
Excellent boss, Helped me a lot, you saved my time

# re: Add a user programmatically to a User Group in SharePoint 4/7/2011 3:28 PM Keyur Parikh
Looks good!

# re: Add a user programmatically to a User Group in SharePoint 6/20/2011 12:02 PM Pradeepa
This one is helped me a lot..thanx buddy....

# re: Add a user programmatically to a User Group in SharePoint 8/29/2012 7:28 AM Sasi Kumar Reddy
Best reference for Adding/Removing users to/from the group:
Note: We have to check before adding/removing users to/from the group and also check the users already exists or not in group.

http://sharepointquicksolutions.blogspot.in/2012/08/addingremoving-users-tofrom-group.html

-Thanks,
Sasi Kumar Reddy

# re: Add a user programmatically to a User Group in SharePoint 8/29/2012 7:28 AM Sasi Kumar Reddy
Best reference for Adding/Removing users to/from the group:
Note: We have to check before adding/removing users to/from the group and also check the users already exists or not in group.

http://sharepointquicksolutions.blogspot.in/2012/08/addingremoving-users-tofrom-group.html

-Thanks,
Sasi Kumar Reddy

Post A Comment
Title:
Name:
Email:
Comment:
Verification: