Skip "Get Started" Page on first startup when creating tenant + site programmatically

Topics: General, Troubleshooting
Oct 17, 2014 at 10:34 AM
Edited Oct 20, 2014 at 8:14 AM
Hello everyone,

i'm currently working on a WebApi controller that creates and initializes a new tenant.

ApiController
        public IHttpActionResult Post(ConfigurationDTO config)
        {
            _tenantService.CreateTenant(config.Tenant);

            //https://orchard.codeplex.com/discussions/275009
            using (var environment = _orchardHost.CreateStandaloneEnvironment(tenant))
            {
                ISetupService setupService = environment.Resolve<ISetupService>();
                setupService.Setup(config.SetupContext);
            }

            return Ok();
        }
My new tenant is in the "Running" state ("Set Up" option in admin menu is not available), but when i open the site for the first time, i get greeted by the "Get Started" page, even though my site should already be configured.
My question is, how does Orchard know whether or not to show the Get Started page?

I found a way to skip the Set Up process:
Open tenants list in admin menu, edit tenant, click save, open page again -> now im on my tenants default orchard homepage, where everything is working as expected.
Oct 24, 2014 at 8:52 AM
Edit: I can't seem to find a way to skip the "Get Started" process without my aforementioned workaround unfortunately. Can anyone tell me, why Orchard doesn't just open my site directly after it's been created in my Controller? If you need more information about my code please tell me. But there is really not much more to it, than what you see in my first post.
Developer
Oct 25, 2014 at 12:11 AM
If you see the setup screen then it means that the setup didn't run correctly. Do you seen any errors in the logs? Are you sure you setup context is configured correctly?
Oct 29, 2014 at 2:40 PM
Edited Oct 29, 2014 at 2:42 PM
First time i checked i only looked into the orchard error log (0 errors). But thanks to your advice i checked the debug log aswell and this is what i got:

2014-10-29 13:57:00,142 [7] Orchard.FileSystems.AppData.AppDataFolder - Default - Could not delete recipe execution folder C:..\Orchard\src\Orchard.Web\App_Data\RecipeQueue\cd24695137504eef8e997f7e7e15a9fd under "App_Data" folder

I debugged the MakeDestinationFileNameAvailable() Method in AppDataFolder class and this is what happens:
/cd24695137504eef8e997f7e7e15a9fd
-0
-1
-2
-3
-4
->files conaint module names in string format.
first the method deletes the 0 file and then tries to delete the folder itself but it seems that it's still in use (see catch comment in MakeDestinationFileNameAvailable()) which is why i get the log message. After that, it deletes 1 - 4 and then tries the cd24695137504eef8e997f7e7e15a9fd folder again (this time successfully).

Don't know if that's what's causing the problem though. What do you think?
Oct 29, 2014 at 4:08 PM
Edited Oct 29, 2014 at 4:21 PM
Edit: Found out that restarting my application also does the trick. But that's not really an option either...
I couldn't find any differences in my SetupContext and ShellSettings objects when creating a tenant programmatically vs manually.

Here is my updated Post Method:
public IHttpActionResult Post(ConfigurationDTO config)
{
    try
    {
        _tenantService.CreateTenant(config.Tenant);

        //https://orchard.codeplex.com/discussions/275009
        using (var environment = _orchardHost.CreateStandaloneEnvironment(config.Tenant))
        {
            ISetupService setupService = environment.Resolve<ISetupService>();
            setupService.Setup(config.SetupContext);
            _orchardHost.Initialize();
        }
        _tenantService.UpdateTenant(_tenantService.GetTenants().FirstOrDefault(t => t.Name == config.Tenant.Name));
        _orchardHost.Initialize();
    }
    catch (Exception e)
    {
        return BadRequest("Tenant creation failed \n" + e.StackTrace);
    }
    return Ok();
}
none of my pathetic code-workarounds helped unfortunately
Nov 5, 2014 at 10:38 AM
Edited Nov 5, 2014 at 10:40 AM
Problem solved (finally!).

This line caused the problem:
_tenantService.CreateTenant(config.Tenant);
what this does is it messes up an if statement in DefaultOrchardHost (which is responsible for restarting a tenant or rather the shell after it's been saved).
        void IShellSettingsManagerEventHandler.Saved(ShellSettings settings) {
            Logger.Debug("Shell saved: " + settings.Name);

            // if a tenant has been created
            if (settings.State != TenantState.Invalid) {
                if (!_tenantsToRestart.GetState().Any(t => t.Name.Equals(settings.Name))) {
                    Logger.Debug("Adding tenant to restart: " + settings.Name + " " + settings.State);
                    _tenantsToRestart.GetState().Add(settings);
                }
            }
        }
what happens now is the "not yet set up" tenant shellsettings are added to _tenantsToRestart, but those are not the shellsettings i want orchard to restart. I wanted the "set up" shellsettings to be restarted. Since there is already a setting entry with the same name it's not added to the list.
As it turns out creating the tenant before calling the Setup() method is unnecessary because it creates the tenant for you, so i simply removed createTenant() from my post-method and voila it now works as expected.

Updated Code
public IHttpActionResult Post(ConfigurationDTO config)
{
    try
    {
        //https://orchard.codeplex.com/discussions/275009
        using (var environment = _orchardHost.CreateStandaloneEnvironment(config.Tenant))
        {
            ISetupService setupService = environment.Resolve<ISetupService>();
            setupService.Setup(config.SetupContext);
        }
    }
    catch (Exception e)
    {
        return BadRequest("Tenant creation failed \n" + e.StackTrace);
    }
    return Ok();
}