Geeks With Blogs
Madhawa Learns To Blog : C#, Java .net, c#, java,sql, OOAD and more mad memory dumps...

        /// <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 .net , MOSS , MOSS Object Model | Back to top


Comments on this post: Add a user programmatically to a User Group in SharePoint

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
Thanks for the post
Left by Bhuvan on Mar 03, 2010 12:04 PM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
Thank you for this (:
Left by Joe Mamma on Sep 16, 2010 2:16 PM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
Excellent boss, Helped me a lot, you saved my time
Left by karthik on Dec 10, 2010 4:16 AM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
Looks good!
Left by Keyur Parikh on Apr 07, 2011 3:28 PM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
This one is helped me a lot..thanx buddy....
Left by Pradeepa on Jun 20, 2011 12:02 PM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
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
Left by Sasi Kumar Reddy on Aug 29, 2012 7:28 AM

# re: Add a user programmatically to a User Group in SharePoint
Requesting Gravatar...
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
Left by Sasi Kumar Reddy on Aug 29, 2012 7:28 AM

Your comment:
 (will show your gravatar)


Copyright © Madhawa Karunaratne | Powered by: GeeksWithBlogs.net | Join free