Start from empty folder to a running Episerver CMS and ECommerce

Assumptions: this post was written at 23/10/2019 with Visual Studio 2019 and Episerver CMS Visual Studio Extension version 11.5.0.412.

The idea is to create a running empty site with CMS and ECommerce.

Ensure that you have Episerer nuget feed configure to https://nuget.episerver.com/feed/packages.svc/

Lets create a new empty Episerver CMS site:


With this we start to create the CMS web application

Choose empty template:

And the simple site folders and files are created with default values.

Lets update the nuget packages by running the following command at the Package Manager Console:

Update-Package EPiServer.CMS -ToHighestMinor

Do the same for the EPiServer.CMS.UI and EPiServer.CMS.TinyMce packages

Using package manage console and selecting only ToHighestMinor allows Episerver packages to be safely installed because it won’t upgrade to a higher major version that would have breaking changes, and non-Episerver dependencies will be updated, as well as the Episerver ones but it won’t accidently install newer but incompatible packages.

By default, this site is using LocalDb database files for demo purpose. Lets create two database using SQLExpress and SQL Server Management Studio.

Create epidemo.cms and epidemo.commerce database.

Now, it’s time to change the connections string to set the new database. For your new epidemo.cms database we can use the following:

.\SQLExpress;Database=epidemo.cms;Trusted_Connection=True;MultipleActiveResultSets=true

Create a new file on the root of the project and named connectionStrings.config with the following content:

<connectionStrings>
  <clear />
  <add name="EPiServerDB" connectionString="Server=.\SQLExpress;Database=epidemo.cms;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>

Find the connectionStrings section at Web.Config and replace it by:

<connectionStrings configSource="connectionStrings.config" />

You may ask if this is necessary for the post purpose, no its not, but i think is always good to learn something else if you did not know how to split the web.config in multiple files.

Now we have conditions to initialize your database, for now it’s only an empty database:

At Visual Studio Package Manager Console, execute the following command:

initialize-epidatabase

Now, the database was populated with necessary for support Episerver CMS.

After initialize the database, its a good idea to update to the latest changes, by executing the following:

update-epidatabase

In this demo, i don’t want to use Windows Integrated authentication, that is set by default. I would like to use SQL database to store the users.

If you look at the tables that were created at database, there is no support for managing the users. So, let’s change the default provider for membership and roleManager. Find those section at web config and change the defaultProvider to SqlServerMembershipProvider and SqlServerRoleProvider.

With this we use Membership. On another post i will change this to use Identity. Here is a tutorial to use Identity

https://world.episerver.com/documentation/developer-guides/CMS/security/episerver-aspnetidentity/

Since we don’t have yet a user created, we can use an initialization module to create a administrator user with the respective role. So, on Business folder create a new folder named Initialization and a class named RegisterAdminInitializationModule

Add the following code to that class:

using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using System.Configuration;
using System.Web.Security;

namespace EpiserverDemo.CMS.Business.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class RegisterAdminInitializationModule : IInitializableModule
    {
        private const string roleName = "Administrators";
        private const string userName = "admin";
        private const string password = "store";
        private const string email = "admin@EpiserverAdventures.com";
        public void Initialize(InitializationEngine context)
        {
            string enabledString =
                ConfigurationManager.AppSettings["EpiserverAdventures:RegisterAdmin"];
            bool enabled;
            if (bool.TryParse(enabledString, out enabled))
            {
                if (enabled)
                {
                    #region Use ASP.NET Membership classes to create the role and user
                    // if the role does not exist, create it
                    if (!Roles.RoleExists(roleName))
                    {
                        Roles.CreateRole(roleName);
                    }
                    // if the user already exists, delete it
                    MembershipUser user = Membership.GetUser(userName);
                    if (user != null)
                    {
                        Membership.DeleteUser(userName);
                    }
                    // create the user with password and add it to role
                    Membership.CreateUser(userName, password, email);
                    Roles.AddUserToRole(userName, roleName);
                    #endregion
                }
            }
        }
        public void Uninitialize(InitializationEngine context) { }
    }
}

Change the Web.Config by adding the following to appSettings:

<add key="EpiserverAdventures:RegisterAdmin" value="true" />

Change the membership provider SqlServerMembershipProvider, to allow a password with minimum 5 letter so we could use the password “store”.

minRequiredPasswordLength="5"

By the way, it is also on this provider that is defined which connection string is used.

Build the project and press Ctrl+F5

To pass by this error, add <clear/> right before the add tag for the profile providers in Web.config

Refresh the page and you will check an 404 error page. Thats good, because since we didn’t yet create the root page, we don’t have a landing page.

At the browser url, add the /episerver after the port number and login with “admin” and “store” as password.


Now it’s time to setup the Ecommerce site.

First we can start to add components to the CMS project. Add the nuget package Episerver.Commerce to the CMS

Add also Episerver.Commerce.UI.ManagerIntegration nuget package to the CMS project.

Review the connection strings that were set on the site:

<connectionStrings>
  <clear />
  <add name="EPiServerDB" connectionString="Server=.\SQLExpress;Database=epidemo.cms;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
  
<add name="EcfSqlConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|EcfSqlConnection_137cc4e0.mdf;Initial Catalog=EcfSqlConnection_137cc4e0;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Change the EcfSqlConnection connection string to point to the epidemo.commerce database:

<connectionStrings>
  <clear />
  <add name="EPiServerDB" connectionString="Server=.\SQLExpress;Database=epidemo.cms;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
  <add name="EcfSqlConnection" connectionString="Server=.\SQLExpress;Database=epidemo.commerce;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>

Now its time to create the new backend site for the Ecommerce.

Create a new empty asp.net web application:

Add to the EpiserverDemo.Commerce project the nuget package Episerver.CommerceManager

Dont cancel the copy of files

For some reason, the project file was not updated:

Include all files into the project, except obj and bin folders

Also add the nuget package EPiServer.ServiceLocation.StructureMap to the Commerce Project.

Copy from the CMS project the connectionString.config into the Commerce project and replace on the Web.Config (Commerce project) the connectionString section with:

<connectionStrings configSource="connectionStrings.config" />

Now run on the Package Manage Console the two commands:

Initialize-EPiDatabase
Update-EPiDatabase 

Now the epidemo.cms and epidemo.commerce have content:

Set the CMS site as start up project and press Ctrl+F5

Login into the episerver (if requested) and click on “Execute all pending steps”.

After migration, go to episerver

For CMS:

For ECommerce:

Now set as startup project the Commerce. Press Ctrl+F5.

If you got the following, please add <clear/> to the Commerce web.config:

Run the Commerce site again: use by default “admin” username and “store” as password

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: