This section describes how to create a Visual Studio project and solution for an installed Sitecore application. There are two different approaches of working with a Sitecore project—inside the web root and outside the web root—and developers can choose either of these approaches based on their requirements. Both the approaches come with their own pros and cons. Sitecore recommends working outside of your web root directory, but for beginners or for the ease of learning the recipes in this book, you can create a project inside the web root directory.
A solution inside the web root is easier to set up and more straightforward, and you will find development changes almost immediately. However, using this approach, you have to maintain your own solution files along with thousands of Sitecore files, so there won't be a clear separation between them, which would make managing a solution very difficult in the long term.
Perform the following steps to create a Web Application project for a new Sitecore installation in Visual Studio 2013:
From Visual Studio, navigate to File | New | Project, which will open a New Project dialog, as shown in the following image:
Select .NET Framework 4.5 or any appropriate framework, as shown in the preceding image.
Select the ASP.NET Web Application project template.
Name the project
SitecoreCookbook
.Enter the web root path in the Location field. For example, if you have installed Sitecore inside the
C:\inetpub\wwwroot\SitecoreCookbook
directory, then create the project inside theC:\inetpub\wwwroot\SitecoreCookbook\Website
directory.Make sure that you uncheck the Create directory for solution checkbox, and then click on the OK button.
It will open another dialog to select project templates. Select the Empty template and check the MVC checkbox, as shown in the following image, and click on OK:
Once the project has been created, close the solution.
We will now move this
SitecoreCookbook
project to the web root directory (to work inside the web root). Navigate to the project directory, for example,C:\inetpub\wwwroot\SitecoreCookbook\Website\SitecoreCookbook
. Move the/Properties
subdirectory, theSitecoreCookbook.csproj
file, and theSitecoreCookbook.csproj.user
file to the parent directory,C:\inetpub\wwwroot\SitecoreCookbook\Website
.Delete the
SitecoreCookbook
subdirectory.Now we will configure this Sitecore web application project. Open the
SitecoreCookbook.csproj
project file in the\Website
directory.Expand the
\Web.config
file, and deleteWeb.Debug.config
andWeb.Release.config
. Similarly, delete all the other missing files from the project.Add references to Sitecore assemblies (
.dll
files) such asSitecore.Kernel
,Sitecore.Client
,Sitecore.Analytics
, and so on from the\Website\Bin
directory, based on your requirements.Set these assemblies' Copy Local property to
False
; otherwise, on building the solution, it will delete Sitecore assemblies from thebin
folder.From Solution Explorer, click on the Show All Files button, which will show that all subdirectories and files exist inside the web root. Select the
/layouts
directory or any other required subdirectories or files and include them in the project and exclude any unwanted subdirectories and files from the project.
So, your changes in the project will get reflected immediately in the Sitecore application. For changes in class files, you need to build the project in order to get it reflected in the application.
A solution outside the web root can make a clear separation between Sitecore files and solution files so that you get a clear understanding of the ownership of files and a clear development solution. Having minimum files in the solution will result in easier source controlling, backing up, and restoring of the solution files. Initially, this setup looks complex to manage and time-consuming to publish changes, but it's beneficial in the long term.
Perform the following steps to create a web application project for a new Sitecore installation in Visual Studio 2013:
Outside the Sitecore web root, create an ASP.NET web application project named
SitecoreCookbook
, for example, at theD:\SitecoreProjects\SitecoreCookbook
location. You can follow the same steps mentioned in the Creating a project in the web root section.Add references to Sitecore assemblies (
.dll
files) such asSitecore.Kernel
,Sitecore.Client
,Sitecore.Analytics
, and so on from the\Website\Bin
directory as needed.Set these assemblies' Copy Local property to
False
.So, we have a clean solution separated from the Sitecore application. Now, you can configure Visual Studio Publish or Web Deploy Publish to save, build, and copy files from this solution to the web root. Right-click on the
SitecoreCookbook
project and click on Publish. Create a new publish profile in the Connection tab, as shown in the following image. Select any Publish method type based on your architecture:Configure the publishing; you need the destination website details based on your selected Publish method type:
Clicking on Next will show you the Settings tab, and you can choose what you want to do on publishing:
From the Preview tab, uncheck directories or files that are not required in the web root, for example, controllers, models, and so on, and click on Publish. This will build the project and copy all the configured files to the destination web root directory.
So, now whenever you make any changes in the project, you must publish them in order to get them reflected in your web root or Sitecore application. So, your Sitecore project and web root will look like the following image:
While working on bigger Sitecore projects, you might need to create a modular architecture. Habitat is an ASP.NET MVC-based Sitecore solution example built on a modular architecture, which you can find at https://goo.gl/naC1hP.