Book Image

Sitecore Cookbook for Developers

By : Yogesh Patel
Book Image

Sitecore Cookbook for Developers

By: Yogesh Patel

Overview of this book

This book will get you started on building rich websites, and customizing user interfaces by creating content management applications quickly. It will give you an insight into web designs and how to customize the Sitecore architecture as per your website's requirements using best practices. Packed with over 70 recipes to help you achieve and solve real-world common tasks, requirements, and the problems of content management, content delivery, and publishing instance environments. It also presents recipes on Sitecore’s backend processes of customizing pipelines, creating custom event handler and media handler, setting hooks and more. Other topics covered include creating a workflow action, publishing sublayouts and media files, securing your environment by customizing user profiles and access rights, boosting search capabilities, optimising performance, scalability and high-availability of Sitecore instances and much more. By the end of this book, you will have be able to add virtually limitless features to your websites by developing and deploying Sitecore efficiently.
Table of Contents (20 chapters)
Sitecore Cookbook for Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating a Visual Studio project


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.

Creating a project in the web root

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:

  1. From Visual Studio, navigate to File | New | Project, which will open a New Project dialog, as shown in the following image:

  2. Select .NET Framework 4.5 or any appropriate framework, as shown in the preceding image.

  3. Select the ASP.NET Web Application project template.

  4. Name the project SitecoreCookbook.

  5. 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 the C:\inetpub\wwwroot\SitecoreCookbook\Website directory.

  6. Make sure that you uncheck the Create directory for solution checkbox, and then click on the OK button.

  7. 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:

  8. Once the project has been created, close the solution.

  9. 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, the SitecoreCookbook.csproj file, and the SitecoreCookbook.csproj.user file to the parent directory, C:\inetpub\wwwroot\SitecoreCookbook\Website.

  10. Delete the SitecoreCookbook subdirectory.

  11. Now we will configure this Sitecore web application project. Open the SitecoreCookbook.csproj project file in the \Website directory.

  12. Expand the \Web.config file, and delete Web.Debug.config and Web.Release.config. Similarly, delete all the other missing files from the project.

  13. Add references to Sitecore assemblies (.dll files) such as Sitecore.Kernel, Sitecore.Client, Sitecore.Analytics, and so on from the \Website\Bin directory, based on your requirements.

  14. Set these assemblies' Copy Local property to False; otherwise, on building the solution, it will delete Sitecore assemblies from the bin folder.

  15. 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.

    Tip

    Debugging your application while having the Show All Files button enabled will either slow down or fail the Sitecore application.

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.

Creating a project outside the web root

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:

  1. Outside the Sitecore web root, create an ASP.NET web application project named SitecoreCookbook, for example, at the D:\SitecoreProjects\SitecoreCookbook location. You can follow the same steps mentioned in the Creating a project in the web root section.

  2. Add references to Sitecore assemblies (.dll files) such as Sitecore.Kernel, Sitecore.Client, Sitecore.Analytics, and so on from the \Website\Bin directory as needed.

  3. Set these assemblies' Copy Local property to False.

  4. 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:

    Note

    Here, publishing means deploying Sitecore application, which is different from the Sitecore item publish.

  5. Configure the publishing; you need the destination website details based on your selected Publish method type:

  6. Clicking on Next will show you the Settings tab, and you can choose what you want to do on publishing:

  7. 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.

  8. 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.