In this training you'll learn a few different patterns for relating site roots and projects. Primarily we'll look at site collections, and nested and sibling projects to show how to configure projects and site roots for code reuse.
We'll do this by setting up a site collection, creating a child and shared project, and templates, using code from both.
First create a folder for your family or collection of sites. This is the best way to group related site roots and projects in an instance. An instance that can grow to have a lot of implementations teams and sites.
Next you'll create a site root.
Take a look at the properties. This allows you to select which project the site root is connected to, and it also enables you to manage your project branches. We’ll create our projects to see that change. We'll talk about branching in another video.
Create your shared and child projects. The shared or sibling project is the best pattern to use if you want a project that will be used with more than one site. The nested or child project is the best approach when you know the project will only be used for one site.
Create one project as a sibling to the site root. Call it Shared Project and add a name for the library called Shared Library. Then create another project, as a child inside of the site root folder. Call this project and library, Child Project and Child Library, respectively. Another look at the properties shows that this child project has been associated with our site root. Now we'll start adding code to these projects so we can see how the shared and child project work together.
We’ll switch over to Visual Studio and use the CDC connection for better developer experience. Now in Visual Studio, open the child project and go to the child library, and select the New menu.
Select C# class, and create MyFirstLibrary.cs.
Now add this code. This will create a couple of methods in your child library here. You use this library code for methods that will be shared across your templates, and across your projects. So we've got two methods here; one to print out hello, and the other one that's going to adjust our path. You now have a site root and two projects.
You've also created a library class file in your child project. So now let's create a template that uses that library code. In Child Project>Templates, select New> Template C# and call your new template, My First Template.
Edit the output.aspx and add this code, that will print out a header and call our one of the methods, that GetHello method from our library file.
Now back in the CMS, go to the site root and select New>File.
We'll create a test page using our template.
Also be sure to choose a workflow always best practice for publishing.
Let's preview to see our rendered output. We see the header and the content printed by the method from the library.
You have now created a template and a test page. In the preview you can see the code from the child library. So now let's return to the shared project.
Eventually you'll use the shared library in your child project- that shared library is an external library- by adding library references.
First we'll go back to the shared library and create and edit a library file called MySecondLibrary.cs. We’ll add this code, which prints GetHello from the shared Library.
Let’s return to the CMS, and go to the child project. Select New>Library Reference and give it a unique name, and create a reference to the shared library.
Also create a reference to the global library folder. We'll do this by creating another library reference and selecting System >Library. We'll do this because if you remember, any instance configured before 2014, prior to the release of the Projects and Sites feature, will have library files located here. You can also think about using this a global library folder (the systemlibrary folder) to share global methods.
Now return to the child project to use the references you have just created. We'll go to the Templates folder in that Child Project. See our references?
We’ll create a new template and call it MySecondTemplate, and edit the output.aspx template file, and add this code that prints content using; first a method from the child project, and then a method from the shared project.
If you look at the namespace references at the top of this template file, just note we've got references to our child library, but also the external libraries that we referenced: the custom library, the system library, and then to the shared library.
Here's our code and methods from both libraries.
We'll go back to the CMS, and back to the site root folder, and create a new file and call it Second Test Page. We'll use our second template, always choose the workflow. When we preview we'll see content printed from both the child project and the shared project.
So, we've now demonstrated the fundamentals of using projects and sites by:
Setting up a site collection
Creating child and shared projects
Creating templates that use code from both projects
Creating library references, so code from both projects could be used in those templates
Watch the next videos to see how projects can help manage template and library code changes using branching and merging.