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. How can I do that? Please help...
2 Answers
Kosty, you can set the welcome page via the WelcomePage
property of the RootFolder
of the new SPWeb
that you're creating:
SPWeb web = GetYourSPWebObject();
web.RootFolder.WelcomePage = "/Pages/Landing.aspx";
web.Update();
If you are trying to achieve this with a page in the Layouts folder in the hive, see Redirect default.aspx or change default landing to page in _layouts folder?
You can place this code in your feature receiver to set the welcome page when the feature is activated (and to set it back when the feature is deactivated).
-
Thanks for the response. Initially there was no welcome page. Setting it up using the above mentioned method doesn't help. Seems like Welcome Page and Default Page are different. Is that so?– K SCommented Feb 5, 2013 at 19:44
-
Do you mean
PublishingWeb.DefaultPage
? "Gets and sets the Welcome page for thisPublishingWeb
object." msdn.microsoft.com/en-us/library/… I don't know if they are or not. You could use reflector if you really wanted to know. If this is aPublishingWeb
there is code in the MSDN link showing how to set this (but it's basically the same). Commented Feb 5, 2013 at 19:58 -
If the page is in the root of the SPWeb (not in the Pages library, etc.), you could set the RootFolder's welcome page to just the name of the page "Page.aspx" (no /'s). Commented Feb 5, 2013 at 19:59
-
The page resides under the site. I have tried these ways already. It is not working. :(– K SCommented Feb 5, 2013 at 20:26
-
I was able to achieve similar functionality via Feature's EventReciever.– K SCommented Feb 6, 2013 at 15:38
Here's the code which worked for me. I put it in Feature's Event Reciever
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);
}
However, my changes don't show up until I disable and then re-enable my feature. Is there a work around for this? Please let me know.
-
This is essentially the same as the code that I posted. If you have a new question, please post it as a separate thread. The sharepoint.stackexchange.com/faq provides a thorough explanation of the rules and expectations of SP-SE. Welcome to the site. Commented Feb 6, 2013 at 15:50
-
Of course your changes don't show up until you re-activate your feature - you put the changes into feature activate event :)– AndreyCommented Nov 13, 2013 at 19:56