0

I am working on a SharePoint server 2013, and using the UI i can do the following:-

  1. Inside the site collection >>site settings >> site permission >>Create Group >> give it a name >> keep the owner as is (my username).
  2. create the group.
  3. edit the group
  4. define that the Group name is the owner.
  5. define that only group members can view the membership.
  6. define that Group owners can add members.
  7. go to a sub-site (which have unique permission), assign the new group and give it Contribute permission.

now i have an event receiver which get fired when an item is updated inside the sub-site. now inside this event receiver i am creating a new sub-site and i want to do the above 7 steps. i start wit the following :-

 public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.SiteId))

                    {

                        string currenweburl = properties.RelativeWebUrl;
                        using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                        {
                            SPWebCollection subWebs = spCurrentSite.Webs;
                            String curListName = properties.ListTitle;

                                //Get the SPListItem object that raised the event
                                    SPListItem curItem = properties.ListItem;
                                //Get the Title field from this item. This will be the name of our new subsite
                                    String curItemSiteName = properties.ListItem["Title"].ToString();
                                    String curItemID = properties.ListItemId.ToString();
                                    SPWeb rootWeb = spCurrentSite.Site.RootWeb;
                                    String webTemplateName = "New Project Template"; //this is the name of the template we will use

                                    //Create the subsite based on the template from the Solution Gallery

                                    SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically", Convert.ToUInt16(1033), webTemplate, false, false);

                                    //Set the new subsite to inherit it's top navigation from the parent site, Use false if you do not want this.

                                    //Code to cover the 7 Steps

                                    site.RootWeb.SiteGroups.Add(curItemSiteName,**** , *****, "Your Group Description");
                                    SPGroup group2 = site.RootWeb.SiteGroups[curItemSiteName];


                                    // Add the group's permissions
                                    SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                    SPRoleAssignment roleAssignment = new SPRoleAssignment(group2);
                                    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                                    site.RootWeb.RoleAssignments.Add(roleAssignment);
                                    site.RootWeb.Update(); 

but i got confused on how i need to implement my last block commented by //Code to cover the 7 Steps .. as seems when creating new group some parameters is SPMember() which other is SPUser().. and not sure where i define the setting to force only members to view the group members. can anyone advice on this please?

2 Answers 2

2

Try adding RoleAssigments to the new web, doing something like this (I edited a bit your code):

public override void ItemUpdated(SPItemEventProperties properties)
{
    base.ItemUpdated(properties);

    SPSecurity.RunWithElevatedPrivileges(delegate ()
    {
        using (SPSite site = new SPSite(properties.SiteId))
        {
            using (SPWeb spCurrentSite = site.OpenWeb(properties.RelativeWebUrl))
            {
                var subWebs = spCurrentSite.Webs;
                var curList = spCurrentSite.Lists.TryGetList(properties.ListTitle);
                if (curList == null) return;

                //Get the SPListItem object that raised the event
                var curItem = curList.GetItemById(properties.ListItemId);

                //Get the Title field from this item. This will be the name of our new subsite
                var curItemSiteName = curItem[SPBuiltInFieldId.Title].ToString();
                var curItemID = properties.ListItemId.ToString();

                // Get the template
                var webTemplateName = "New Project Template"; //this is the name of the template we will use
                var webTemplate = ****;

                //Create the subsite based on the template from the Solution Gallery
                SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically", Convert.ToUInt16(1033), webTemplate, false, false);

                //Set the new subsite to inherit it's top navigation from the parent site, Use false if you do not want this.
                // *****

                //Code to cover the 7 Steps

                site.RootWeb.SiteGroups.Add(curItemSiteName, **** , ****, "Your Group Description");
                var group2 = site.RootWeb.SiteGroups[curItemSiteName];

                //edit group settings
                group2.AllowMembersEditMembership = true;
                group2.OnlyAllowMembersViewMembership = true;
                group2.Owner = group2;
                group2.Update();

                // break Inheritance
                newSite.BreakRoleInheritance(false);

                // Add the group's permissions
                var roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
                var roleAssignment = new SPRoleAssignment(group2);
                roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                newSite.RoleAssignments.Add(roleAssignment);
                newSite.Update();
            }
        }
    });
}

Do not use properties.ListItem inside the SPSecurity.RunWithElevatedPrivileges (security is loaded from the context). You need to get a new instance of the list item.

6
  • thanks for the great reply.. so in this case group2 will be added to the RootWeb with empty permission .. while it will be assigned a Contribute permission on the newSite is this correct ? second question now i am using "properties.ListItem" inside the "SPSecurity.RunWithElevatedPrivileges" because in my case what i really want to elevate the permission on is "spCurrentSite.Webs.Add()" . but for reading the item and even updating it i am not worried about elevating the permission, as the current user will always have the permission to do so..
    – John John
    Commented Jan 15, 2017 at 1:33
  • ....so i think it is fine to use "properties.ListItem" inside the "SPSecurity.RunWithElevatedPrivileges" in my case is this correct ? or there are other reasons which should prevent me from this? can you adivce on my 2 questions please?
    – John John
    Commented Jan 15, 2017 at 1:33
  • 1
    Yes, group2 will be added to the RootWeb with empty permission, while it will be assigned a Contribute permission on the newSite. The reason for using a new elevated instance of the list item is to prevent future issues, with maybe another user which could not have enough permissions to create webs.
    – Cecilia
    Commented Jan 15, 2017 at 16:51
  • 1
    I mean, the reason for using a new elevated instance of the list item is to prevent future issues with permissions, but only the the code changes. If it will not, you don't have to change it.
    – Cecilia
    Commented Jan 15, 2017 at 17:03
  • 1
    Yes, you can continue to use "properties.ListItem" if you want, but I prefer to use the elevated instace of it...
    – Cecilia
    Commented Jan 16, 2017 at 10:25
1

Try below code:

 //break new site inheritance
newSite.BreakRoleInheritance(true);
site.RootWeb.SiteGroups.Add(curItemSiteName,web.SiteUsers[@"domain\user"], web.SiteUsers[@"domain\user"], "Your Group Description");
//site.RootWeb.SiteGroups.Add(curItemSiteName,web.SiteUsers[@"domain\user"], null, "Your Group Description");

SPGroup group2 = site.SiteGroups[curItemSiteName];

//add group to subsite with contribute permissions
SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleAssignment roleAssignment = new SPRoleAssignment(group2);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
newSite.RoleAssignments.Add(roleAssignment);
newSite.Update();

//edit group settings
group2.AllowMembersEditMembership = true;
group2.OnlyAllowMembersViewMembership = true;
group2.Owner = group2;
group2.Update();
newSite.Update(); 
3
  • now your code will work, but there is still 2 issues (related to step 7), as in your code the new group will be added to the Root site only. but i need to also assign this new Group to the newly created sub-site "newSite" object in my case.. so how i can do so ? now from the UI i can go to the sub-site>>Site settings>>Permissions>Grant Permission .. Also i want the new group to have non-permission on the root site, while to have Contribute on the sub-site "newsite" in my case? as my sub-site have unique permission. is this possible?
    – John John
    Commented Jan 13, 2017 at 13:54
  • can you please advice on my above comment please?
    – John John
    Commented Jan 15, 2017 at 1:25
  • 1
    Check updated code. Commented Jan 16, 2017 at 9:30

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.