This project is read-only.

Setting the homepage programmatically

Topics: Core, Customizing Orchard, Writing modules
Aug 21, 2014 at 6:26 AM
Hi guys,

I need some help to try and figure out how I can set the homepage in an Orchard site programmatically. I've done some searching and the best that I could find is this bit of code but it doesn't seem to work for me. No errors whatsoever but it just doesn't work. Could I be missing something?
var homePage = _contentManager.Create("Page");
homePage.As<TitlePart>().Title = HomePageTitle;
homePage.As<BodyPart>().Text = String.Empty;

var homeRoute = homePage.As<AutoroutePart>();
//homeRoute.CustomPattern = string.Empty;
homeRoute.DisplayAlias = string.Empty;
If someone could help point me in the right direction, that would be much appreciated.

Aug 22, 2014 at 5:01 AM
I think you also have to actually publish the content item, e.g.
Aug 22, 2014 at 5:13 AM
Thanks and sorry I didn't include that bit in my post. I have actually published the home page but it doesn't work for me. It just displays an error page with the message "The resource cannot be found." I noticed the option to create a page while making it the homepage is available using commands but I don't, for the life of me, know how I could include that option in the form of an attribute or some sort.
page create /Slug:"welcome-to-orchard" /Title:"Welcome to Orchard!" /Path:"welcome-to-orchard" /Homepage:true /Publish:true /UseWelcomeText:truetrue
Aug 22, 2014 at 5:59 AM
Ah OK. So how about instead of setting homeRoute.DisplayAlias to an empty string, you set it to "/"? According to the PublishAlias method in Orchard.Autoroute.AutoroutePartHandler, it should pick up on that value, unmark any other existing homepage as being the homepage, and invoke the PublishAlias method on IAutorouteService.
Aug 22, 2014 at 6:30 AM
I tried setting the DisplayAlias to string.Empty and "/" but those didn't seem to work as well. I'm gonna have to revisit the code and see if I'm missing anything else.
Aug 27, 2014 at 6:57 AM
It should work, this code works for us:
             ContentDefinitionManager.AlterTypeDefinition("Empty", cfg => cfg

            var empty = _contentManager.New("Empty");
            empty.As<AutoroutePart>().DisplayAlias = "/";