Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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...

share

2 Answers 2

up vote 0 down vote accepted

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"
share
    
What is WebName in line # 2? Can you please tell me? –  Kosty Feb 6 '13 at 19:32
    
The name of your SPWeb. Like if you created a new "Site" through the UI and called it "Accounting", the Name of the SPWeb object would be "Accounting" see: msdn.microsoft.com/en-us/library/… –  RJ Cuthbertson Feb 6 '13 at 21:10

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"); ;
            }
        }
    }
}
share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .