Changing the SharePoint Home Page doesn't work until I click on site Title
-
10-12-2019 - |
Question
I am installing a sub-site in Sharepoint 2010 using PowerShell. For all the sub-sites, we have configured sharepoint to launch a particular sub-site's default.aspx page as landing page after log-in. For my use case, I want override this behavior by launching a page being created as a part of a Feature used in this sub-site. I am doind so by using following code in Feature's EventReciever.cs :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (var currentWeb = properties.Feature.Parent as SPWeb)
{
if (currentWeb != null)
{
var root = currentWeb.RootFolder;
if (root != null)
{
root.WelcomePage = @"Management\Pages\LandingPage.aspx";
root.Update();
}
//this.AddNavigationNodes(currentWeb);
}
}
base.FeatureActivated(properties);
}
The code doesn't work until either I click on the title of the site (on top left), or enable and then re-enable the feature. Seems like code needs some kind of triggering event. Am I doing something wrong? Please help...
Solution
This code in your feature event receiver will run after the completion of your feature's activation.
From MSDN SPFeatureReceiver.FeatureActivated method:
Handles the event that is raised after a Feature is activated.
and
This method is called when a Feature is activated.
If you'd like to arbitrarily set your welcome page you could user PowerShell like:
$site = Get-SPSite -Identity "http://server:port/";
$web = $site.OpenWeb("WebName");
$web.RootFolder.WelcomePage = "/Path/Page.aspx";
$web.Update();
$web.Dispose();
$site.Dipose();
Write-Host "Successfully updated the Welcome Page"
OTHER TIPS
Another alternative would be to intentionally wait until the user hits the site and use a control injected into additional page head that sets the value, redirects the user, and deactivates itself.
Elements file:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control
Id="AdditionalPageHead"
Sequence="1000"
ControlSrc="~/_controltemplates/MySites.Branding/WebPartInjector.ascx">
</Control>
</Elements>
code behind for Injector.ascx:
public partial class WebPartInjector : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
using (new SPMonitoredScope("MySites.Branding WebPartInjector Page_Load"))
{
SPWeb curWeb = SPContext.Current.Web;
try
{
//*******DO STUFF HERE********
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
curWeb.Features.Remove(new Guid("This-is-where-you-put-the-guid-of-the-feature"));
}
catch
{
//swallowing deactivation of self error
}
});
curWeb.AllowUnsafeUpdates = false;
Context.Response.Redirect(curWeb.Url);
}
catch (Exception g)
{
Logger.LogError(g, "WebPartInjector"); ;
}
}
}
}