Ticket (Solved)

How to create AutoRoute Patterns by code?

Hey guys, I have a custom module that I'm developing along with a theme -- as I'm developing I find it a real timewaster adding autoroutes to my page type each time I delete the DB or start afresh. So how do I go about adding my AutoRoute Patterns by code on my modules migration? Do I use IContentDefinitionManager? If so, how do I make a new oneā€¦

            var pageType = _contentDefinitionManager.GetTypeDefinition("Page"); //9
        var autoPart = _contentDefinitionManager.GetPartDefinition("Autoroute");//15

        //New ContentTypePartDefinitionRecord?

Thanks in advance, Orlando

Re: How to create AutoRoute Patterns by code?

I think that you can add a migration step to your module with something like this:

public int UpdateFromX() {
    ....
    ContentDefinitionManager.AlterTypeDefinition("Page",
        cfg => cfg
        ...
        .WithPart("AutoroutePart", builder => builder
            .WithSetting("AutorouteSettings.AllowCustomPattern", "True")
            .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "False")
            .WithSetting("AutorouteSettings.PatternDefinitions", "[{\"Name\":\"Title\",\"Pattern\":\"{Content.Slug}\",\"Description\":\"my-page\"}]"))
        ...

        return X+1;
    }

You also have the _autorouteService.CreatePattern() that you can use through a controller, see in Upgrade/RouteController.cs for an example.

Finally you have the CreatePattern() command that you can use through the command line, see in Orchard.Autoroute.AutorouteCommands.cs. So, i think you can create a custom recipe that executes this command.

The simpler seems to be the migration step that is executed when you first enable your feature.

Best

Sunday, February 14, 2016 12:45:16 AM byjtkech
  • jtkech
  • Lv. 13 Rookie
  • Total EXP: 1028

Re: How to create AutoRoute Patterns by code?

Thanks ever so much @jtkech - worked perfectly...I went with the migration method :)

Friday, February 19, 2016 2:19:58 AM byoakorc
  • oakorc
  • Lv. 02 Rookie
  • Total EXP: 55

Post a reply

You need to be signed in to post a reply.

Sign In