modify my sub-site Quick Launch link title from “Home” to be equal to the Sub-site title, inside my event receiver

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/203266

Question

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

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top