คำตอบ how to create a Group in sharepoint 2010?

  • Thursday, August 09, 2012 12:56 PM
     
     

    i want to create a SPGroup and give the permission to the current logged in user to that group programatically.Also the current logged in user should be added to that group programatically.How can this be done?Can somebody provide the code for the method?It will all be done on a button click.

    Please suggest

    thanks

    Suu30  

All Replies

  • Thursday, August 09, 2012 1:27 PM
     
     

    Hello Suu30,

    refer the below links

    http://sharepoint.infoyen.com/2012/06/24/create-group-permission-and-assign-it-programmatically/

    http://sharepoint.stackexchange.com/questions/42044/create-new-sharepoint-group-programmatically

    http://sharepointex.blogspot.in/2007/08/my-test-comment.html


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • Thursday, August 09, 2012 1:29 PM
     
      Has Code

    Hello,

    I belive this post can help you

    http://www.learningsharepoint.com/2010/09/03/programmatically-create-user-groups-sharepoint-2010/

    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010customization/thread/d11f1488-15f9-4afd-9c07-04fa070daa10/

    also to get the current user you can use 

    SPContext.Current.Web.CurrentUser

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

  • Friday, August 10, 2012 6:36 AM
     
     

    for creating group,giving permission and adding the current user I am using this code

           

    using (SPSite siteColl = new SPSite(socialDataStatsSite))
                        {
                        SPWeb web = SPContext.Current.Web;
                        SPUser spUser = siteColl.RootWeb.CurrentUser;
                        web.AllowUnsafeUpdates = true;
                        web.SiteGroups.Add("NewBie", web.CurrentUser, web.CurrentUser, string.Empty);

                        SPGroup group = web.SiteGroups["GroupName"];
                        SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                        SPRoleAssignment roleAssigment = new SPRoleAssignment(group);
                        roleAssigment.RoleDefinitionBindings.Add(roleDefinition);
                        web.RoleAssignments.Add(roleAssigment);
                        web.Update();

                      
                        group.AddUser(spUser);

                        web.AllowUnsafeUpdates = false;

    group is being added but the current user is not getting added.and when i sign in as different user it s giving access denied.Where am i going wrong?Please suggest

    thanks

    Suu30


    • Edited by Suu30 Wednesday, December 05, 2012 1:13 PM
    •  
  • Friday, August 10, 2012 7:47 AM
     
      Has Code

    Hello,

    Try wrapping your code in RunWithElevatedPrivileges method

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
         using (SPSite siteColl = new SPSite("http://spdev01/"))
                {
                    SPWeb web = SPContext.Current.Web; ;
                    SPUser spUser = siteColl.RootWeb.CurrentUser;
                    web.AllowUnsafeUpdates = true;
                    web.SiteGroups.Add("NewBie3", web.CurrentUser, web.CurrentUser, string.Empty);
    
                    SPGroup group = web.SiteGroups["NewBie3"];
                    SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    SPRoleAssignment roleAssigment = new SPRoleAssignment(group);
                    roleAssigment.RoleDefinitionBindings.Add(roleDefinition);
                    web.RoleAssignments.Add(roleAssigment);
                    web.Update();
    
    
                    group.AddUser(spUser);
    
                    web.AllowUnsafeUpdates = false;
                }
    });
    I ran your code on my site and it worked fine, so im guessing it could be an issue with current users permmisions, wich should be taken care of by code above.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

  • Friday, August 10, 2012 8:36 AM
     
     

    thank u bgrgic,

    I have used the above code also,but getting the same result.the users are not getting added.I am trying to add a user in the "Group member" permission having the contribute permission but again it is giving a access denied error. In my site the user name is shown as "System Account" and not my name.Is that a reason for the problem?

    what kind of changes should be made in the current user permissions?please suggest

    I have attached a snap shots for your reference.

    thanks

    Suu30


    • Edited by Suu30 Friday, August 10, 2012 9:07 AM
    •  
  • Friday, August 10, 2012 9:24 AM
     
     Answered Has Code
    RunWithElevatedPrivileges

    should take care of any permmision issues (because the code is then run under sharepoint sys account).

    You can also try adding EnsureUser

    SPUser spUser = web.EnsureUser(siteColl.RootWeb.CurrentUser.LoginName);

    Also I'm not sure you should be using 

    SiteGroups

    you can try using Groups. You can read about the difference here:

    http://www.mindfiresolutions.com/SharePoint-Groups-vs-SiteGroups--Mindfire-Solutions-955.php


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.