0

I am working on building a new event receiver which get fired when item is updated, inside my sharepoint enterprise server 2013.

now inside the event receiver i am doing the following main steps:-

  1. create a new sub-site.
  2. set its master page.
  3. stop inheriting permission
  4. then remove a group named "rootUserOnly" . as those users should not be able to access the newly created sub-site.

so my event receiver ended up with the following code:-

   public override void ItemUpdated(SPItemEventProperties properties)
        {
               base.ItemUpdated(properties);
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite site = new SPSite(properties.SiteId))

       {

            string currenweburl = properties.RelativeWebUrl;//string currentweburl3 = properties.Web.WebTemplate;
            using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
            {

                    //code goes here....
                    //1 Create the subsite based on the template from the Solution Gallery
                    SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, false, false);


                    //2 inherate navigation from parent
                    newSite.Navigation.UseShared = true;

                    //3 set its master page to a custom.master
                    string masterPage = "Custom.master";
                    var masterUri = new Uri(rootWeb.Url + "/_catalogs/masterpage/" + masterPage);
                    newSite.MasterUrl = masterUri.AbsolutePath;
                    newSite.CustomMasterUrl = masterUri.AbsolutePath;

                    //4 break the permission inheritance
                    newSite.BreakRoleInheritance(true,true);

                    //5 remove from this subsite a group named "rootUsersOnly"
                    SPGroup group = newSite.SiteGroups["rootUsersOnly"];
                    newSite.RoleAssignments.RemoveById(group.ID);



                    //update then dispose
                    newSite.Update();
                    newSite.Close();





                }
            }
        }
   });
//
        }


    }
}

but i am not sure if i am doing this in the correct way ? now i did a test and the sub-site was created successful + it got the correct master page + its have unique permission + the security group named "rootUsersOnly" does not exists inside the sub-site. so can anyone advice on this please ?

1

Replace the line as below:

//code goes here....
//1 Create the subsite based on the template from the Solution Gallery
SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, true, false);

By setting it to true, you will inherit unique permission.

Explanation -

useUniquePermissions - set it to true to create a subsite that does not inherit permissions from another site.

When you do that, you can remove the below line:

//4 break the permission inheritance
newSite.BreakRoleInheritance(true,true);

Other than that, code looks good.

Reference - SPWebCollection.Add

  • so you mean my current appraoch should be fine also but i am adding extra nu-necessary step of breaking the role inheritance ,, which i can do when creating the new sub-site and pass "true,false" instead of "true,true" ? is this correct? – john Gu Dec 7 '16 at 12:13
  • yes thats correct. Modify step 1 as given and remove step 4. – Gautam Sheth Dec 7 '16 at 12:23
  • your approach will not work the same !! now if i modify my code as you mentioned i will end up without any security groups on the new subsite,, and only the "system account" is granted to access the new subsite.. while using my appraoch the new sub-site will have the all the security groups from his parent ,, except the rootUsersOnly... so can you adivce if your appraoch works differently at your end ? – john Gu Dec 7 '16 at 15:55
  • can you adivce on my above comment please? – john Gu Dec 9 '16 at 0:08
  • Leave it as it is then. Tested the code at my end as well , spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, true, false); , when this is used it removes the sharepoint groups as well. So your current code is alright. – Gautam Sheth Dec 21 '16 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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