1

I am working on a sharepoint 2013 enterprise server. and i have the following case:-

1.I created an event receiver which will get fired when an item is updated for the first time.

2.the event receiver will create a new sub-site as follow:-

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();

now the above is working well, where the sub-site will be created with the correct settings. but i am facing this problem.

  1. when the new sub-site is created, there will be a Quick Launch link labeled as "Home" as follow:-

enter image description here

  1. so can i change this title from "Home" to be equal to the new sub-site's Title inside my event receiver ?

Thanks

1

Add below code to update quick launch:

SPNavigationNodeCollection nodes = newSite.Navigation.QuickLaunch;
foreach (SPNavigationNode node in nodes)
{
    if (node.Title == "Home")
    {       
        node.Title = newSite.Title;
        node.Update();
        newSite.Update();
        break;
    }
}

So, your full code would be as below:

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();

SPNavigationNodeCollection nodes = newSite.Navigation.QuickLaunch;
foreach (SPNavigationNode node in nodes)
{
    if (node.Title == "Home")
    {       
        node.Title = newSite.Title;
        node.Update();
        newSite.Update();
        break;
    }
}

newSite.Close();
  • worked well,, thanks.. but i have a minor question is there a reason why you are issuing two newSite.Update(); commands instead of one ? – john Gu Dec 22 '16 at 15:06
  • Inside the quick launch loop, I am using node.update and then newsite.update . I am using only one newsite.update – Gautam Sheth Dec 22 '16 at 15:15

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.