In this post I’m going to set up a new Sitecore solution with TDS. I’ll then enable code generation to create glass mapper compatible objects.
First you’ll want to download the latest version of Sitecore from the SDN. At the time of writing this is Sitecore 7.2 rev. 140526. You’ll then need to download TDS from here.
Before getting started with the solution attach the Sitecore databases in SQL Server Management Studio and set up a user to be able to access each of the databases. If you don’t have Sitecore Rocks installed already install it from here. The final thing to do before creating the project is install Sitecore TDS connector. This will connect rocks and TDS and automatically manage your Sitecore item files are you create and change them through Sitecore Rocks. After installing Rocks, you’ll have a Sitecore menu in Visual Studio. From this menu choose Manage Plugins. In the dialog select the Sitecore Rocks MyGet Gallery from the left then install the TDS Connector.
Now let’s create a new Sitecore solution and project. It can be a WebForms or MVC project, but for this post I’m going to use MVC. Sitecore 7.2 uses MVC 5 so if you’re using Visual Studio 2012 you’ll want to first install ASP.NET and Web Tools 2013.1 for Visual Studio 2012. This will give you the template to create an MVC 5 project. Create new Empty MVC 5 project, I’ve called mine MySiteCoreWebsite.Web with the solution name MySitecoreWebsite.
Sitecore 7.2 uses MVC 5.1 but the solution created will reference 5.0. This can easily by upgraded to 5.1 by typing ‘Install-Package Microsoft.AspNet.Mvc -Version 5.1.0’ into the Package Manager Console.
After creating the project I now want to remove the files and folders that I don’t need. It’s best practice to only have your own files and those that have been change in your web project and source control. TDS will copy these files into the directory containing all of the vanilla Sitecore files. The image below shows what I have removed.
Now I’m going to put the Sitecore Website folder into my solution folder. I’m also going to copy the Data folder into the Website folder as this is the default path used by Sitecore. This can be changed later. At the end of the post I’ll share my .gitignore file which stops the Website folder and other files from being checked into source control. My solution folder now looks like this:
At this point I’m going to create a website in IIS that points to the Website folder. I’ve used the hostname mysitecorewebsite.local and created an entry in my hosts file for this.
Now I’m going to create a TDS project for my master database. Using my naming convention this new project is called MySitecoreWebsite.TDS.Master. You may also want a separate project for the core database if you add any items to it, and maybe even an initial content project just containing enough content to get a new developer started on a project. For now I’m just going create the master project.
Right-click on the new TDS project and choose Properties. In the General section set the Source Web Projects to be MySitecoreWebsite.Web. This enables TDS to get the build output from that project and copy it to the Sitecore Website folder. Next go to the Build section and set the Sitecore Web Url and Sitecore Deploy Folder. Here I’ve used the relative path ‘../Website’ which would allow this solution to work wherever a developer puts it on their machine. Finally check the Install Sitecore Connector checkbox. This will add a folder called _DEV to the Website folder containing the TDS webservice used for deploying items.
Next I want to add the first file to my website project which will be the connection strings for Sitecore to use. As I mentioned before TDS will take the compiled code from this web project and copy it into the Sitecore Website directory, so I need to mirror that folder structure. The connection strings live in App_Config/ConnectionString.config so I first want to create this folder and copy in the existing file from Sitecore. I can then update the file to set the correct connection strings.
You can now build the solution and you should find after the build that the connection string file from your web project has now been copied to the Sitecore Website folder. You should now be able to access the website via whatever URL was set up, for me http://mysitecorewebsite.local.
So now that TDS is configured it’s time to set the items we want it to keep track of. Click the Sitecore menu item in Visual Studio and choose new connection. Fill out the necessary fields as below.
Click the Test button. If using the Hard Rocks option it will want to copy some extra files to your website’s bin directory. Just follow the steps and the new connection should be set up.
Open Sitecore explorer and open the new connection that was just set up. Open the mater database node and you should see the same tree structure as you would see in Sitecore’s Content Editor. I’m going to create a new template folder called Custom as shown below.
I now need to tell TDS that I want it to synchronize this folder along with any child items. This is by right clicking on the TDS project and selecting ‘Get Sitecore Items’. If the TDS connector has not yet been set up it will prompt you to install it then open the dialog. Here I navigate to templates and select my Custom folder. I then right click on that folder and choose ‘Select all children’.
There are no children yet, but this will ensure TDS will serialize any children I add to this folder. You should see in the TDS project that it’s adding a templates node and our ‘Custom’ folder node.
Now I’m going to add another template folder called ‘Services’ inside’ Custom’ and a new template in that called Service; I’ll use this template in later blog posts.
As soon as the new items are created you should see it automatically appear in solution explorer.
The final part of this tutorial is to set up the code generation. The templates are for use with Glass Mapper so first I need to install it via Nuget. Right click on the web project and select Manage Nuget Packages then search for ‘Glass Mapper’. There are quite a few different packages, for now just install ‘Glass.Mapper’ which is the core and ‘Glass.Mapper.Sc.CastleWindsor’ which is the Sitecore package preconfigured with Castle Windsor.
The generated classes also reference some Sitecore libraries. For these I create a folder in my solution folder called ‘lib’ and copy the libraries I need here. I can then reference them in my web project. This folder is checked in to source control. For now the only library required is ‘Sitecore.Kernel.dll’. Copy it from the ‘Sitecore Website/bin’ folder into the new ‘lib’ folder then make a reference to it in the web project.
The code generation templates are written by Mike Edwards and are on github here. Clone the repository and look in Sitecore.Master > Code Generation Templates. You only need 6 of these templates: GeneralExtensions.tt, GlassV3Header.tt, GlassV3Item.tt, Helpers.tt, Inflector.tt and StringExtensions.tt.
Right click on the TDS project and select Properties, then select the Code Generation section. Check the Enable Code Generation checkbox and select the web project in the Target Project dropdown. Fill out the Code Generation Target File and Base Namespace fields. I like to put the classes inside the Models folder with the base namespace ‘Models’.
Save the settings with the transform file dropdowns empty. You’ll see in the TDS project a new node called Code Generation Templates. Drop the 6 templates I mentioned above into here. Now in the properties window set the Header Transform File to ‘GlassV3Header.tt’ and the Base Project Transform File to ‘GlassV3Item.tt’. Save the settings and it should automatically generate your classes. As it’s the first time generating you may get an error that the file already exists. Just show all files in Solution Explorer and you’ll see the generated class file in the Models folder. Add this to the project and you should be able to continue to generate the file without the error.
Now any time you create items in Sitecore Rocks TDS will serialize them and generate a Glass Mapper class for them. If you need to manually regenerate the classes you can do so by right clicking on the TDS project and choose ‘Re-Generate Code for all items’