Showing posts with label CMS. Show all posts
Showing posts with label CMS. Show all posts

Thursday, January 24, 2019

Create Blog home page content in Umbraco CMS

You must have already followed previous articles to continue as below instructions. In case if you have jumped to this article, you may follow below article first and prepare your solution to continue along with the code snippets. You can view a static content of blog home page in startbootstrap-blog-home.

  1. Getting started with Umbraco CMS 7+
  2. Install Umbraco on Localhost and Build Visual Studio Solution
  3. Create a Master Template(layout) in Umbraco CMS part 1
  4. Create a Master Template(layout) in Umbraco CMS part 2
Create a HomeController and Create a partial view with following Model.

using System;
namespace UmbracoDemoApp.Models
{
    public class PostSummaryModel
    {
        public PostSummaryModel(string imageUrl, string title, string postedBy, DateTime postedOn, string content)
        {
            ImageUrl = imageUrl;
            Title = title;
            PostedBy = postedBy;
            PostedOn = postedOn;
            Content = content;
        }

        public string ImageUrl { get;  }
        public string Title { get;  }
        public string PostedBy { get; }
        public DateTime PostedOn { get; }
        public string Content { get; }
    }
}
Your home controller should look like this. Following code will iterate all the blog posts under home tab. We will create placeholders in a while in umbraco portal.
 public class HomeController : SurfaceController
    {
        private const string VIEW_PATH = "~/Views/Home/{0}";
        public ActionResult RenderPostsSummaries()
        {
            var list = new List<PostSummaryModel>();
            IPublishedContent blogPage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();

            foreach (IPublishedContent item in blogPage.Children)
            {
                string title = item.GetPropertyValue<string>("postTitle");
                string imgUrl = Umbraco.Media(item.GetPropertyValue<string>("primaryImageUrl"));
                string content = item.GetPropertyValue<string>("PostContent");
                if (!string.IsNullOrEmpty(content))
                {
                    if (content.Length > 200)
                        content = content.Substring(0, 200);
                }
                else
                    content = string.Empty;

                if (string.IsNullOrEmpty(imgUrl))
                    imgUrl = "http://placehold.it/750x300"; // custom image

                list.Add(new PostSummaryModel(item.Url, imgUrl, title, item.CreatorName, item.CreateDate, content));
            }
            return PartialView(string.Format(VIEW_PATH, "_postsSummary.cshtml"), list);
        }
    }
As you have already noticed, I have used a _postsSummary.cshtml partial view to render post summaries. Create a partial view and paste the following code to read the posts from the Db.

@using UmbracoDemoApp.Models
@model List<UmbracoDemoApp.Models.PostSummaryModel>
<div>
    @if (Model.Count == 0)
    {
        <div style="height:100px;" class="justify-content-center">No posts to display</div>
    }
    else
    {
        foreach (PostSummaryModel b in Model)
        {
            @RenderPostSummaryView(b);
        }
    }
</div>

@helper  RenderPostSummaryView(PostSummaryModel item)
{
    <!-- Blog Post -->
    <div class="card mb-4">
        <img class="card-img-top" src="@item.ImageUrl" alt="Card image cap">
        <div class="card-body">
            <h2 class="card-title">@item.Title</h2>
            <p class="card-text">@item.Content</p>
            <a href="@item.PostUrl" class="btn btn-primary">Read More &rarr;</a>
        </div>
        <div class="card-footer text-muted">
            @item.PostedOn
            <a href="#">@item.PostedBy</a>
        </div>
    </div>
}
As the last step update Home.cshtml page with following line of code. It will read the post summaries and render inside the page body.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "Master.cshtml";
}

@{ Html.RenderAction("RenderPostsSummaries", "Home");}

We are almost done with the C# coding in Visual studio project. Next is to create place holders in home page and create couple of blog posts to show in home page.

Login to http://localhost/UmbracoDemoApp/umbraco,

go to Templates--> Master--> Create BlogHome then
go to settings --> Document Types --> Home--> Permissions --> Allow as root and add BlogHome as a Child.

Add 3 tabs for Page Title, Primary Image and for the content of BlogHome page. Make sure you name the control as you specified in the code. title, primaryImageUrl, and postContent then save.When you add controls, add reuse component tab to add a textString for title, Media Picker for image and Rich TextEditor for post content.

Now you can create blog posts under content tab. Navigate to the content tab --> Create --> BlogHome as the template --> Fill the content --> save and publish.


Create a Master Template(layout) in Umbraco CMS part 2

If you have followed the steps given in Create a Master Template(layout) in Umbraco CMS part 1 you should be able to see the demo application with the integrated template without any run-time errors. You can refer previous posts on Getting started with Umbraco CMS 7+ post.

In this post we will separate header, footer, sidebar and other widgets into partial views using visual studio.

In the startbootstrap home template, we can separate out following areas into partial views.
  1. Search Widget
  2. Categories Widget
  3. Side widget
  4. Header
  5. Footer
Let's create an empty controller for "MasterLayout" under controllers folder. Then add following partial views as shown below. Note that, MasterLayoutController has been extended with SurfaceController.
  
using System.Web.Mvc;
using Umbraco.Web.Mvc;

namespace UmbracoDemoApp.Controllers
{
    public class MasterLayoutController : SurfaceController
    {
        private const string VIEW_PATHS = "~/Views/MasterLayout/{0}";
        public ActionResult RenderHeader() => PartialView(string.Format(VIEW_PATHS, "_header.cshtml"));

        public ActionResult RenderFooter() => PartialView(string.Format(VIEW_PATHS, "_footer.cshtml"));

        public ActionResult RenderSearch() => PartialView(string.Format(VIEW_PATHS, "_search.cshtml"));

        public ActionResult RenderCategories() => PartialView(string.Format(VIEW_PATHS, "_categories.cshtml"));

        public ActionResult RenderSideWidget() => PartialView(string.Format(VIEW_PATHS, "_sidewidget.cshtml"));
    }
}

Next is to move the related html code snippets from master template to relevant partial views. If you have correctly moved the HTML code, your Master.cshtml page should look like this.
We have completed the master page layout with partial views. Next is to create the blog post partial view in Home page.

Wednesday, January 16, 2019

Create a Master Template(layout) in Umbraco CMS part 1

This is the second tutorial of Getting started with Umbraco CMS 7+ series. If you have already installed Umrbaco in your development machine, you may continue in this tutorial. If you haven't installed you may go through Install Umbraco on Localhost and Build Visual Studio Solution article before proceeding.

In this article we are going to integrate a simple blog template to our application. You can download the a free bootstrap template that I'm using for this tutorial in startbootstrap.com. For this tutorial, two templates are going to be referred for blog home and blog post.

Step 01: Extract the downloaded files into a folder

You should be able to see following folders and files as shown below.


Step 02: Let's Create a View for master layout in Umbrao Portal.

Login to you UmbracoCMS back-office as an Administrator. If you have followed my previous article the default url of the back-office would be http://localhost/UmbracoDemoApp/umbraco 
Go to Settings --> Templates --> Create --> Name the template as Master --> Save

Let's move to the Visual Studio to set up the rest of the things in the application. To view the created Master template, click show all files then include the Master.cshtml.

Step 03: Copy HTML code from the template
  1. Open the "startbootstrap-blog-home-gh-pages" folder. 
  2. Open the "index.html" page in notepad and copy the whole content.
  3. Paste the content inside Master.cshtml (Do not overwrite the existing content in the Master template)
If you have correctly copied the content final output of the HTML code should look like this.
There are 4 areas in the template. In future, we will separate those areas into Partial Views. 
  1. head
  2. body content (dynamic content area)
  3. side bar
  4. footer 
Search  for Blog Entries Column comment in the html and add  @RenderBody() code snippet just below the comment. This is the area where recent blog posts or the dynamic content going to be rendered in the application.

Let's move vendor and css folder to the Visual studio solution from the downloaded template. Make sure you prefix all src and href properties of style-sheets and Js in the Master template file with "~/".

Note: No need to prefix src and href which have "#" or "".

You are almost done with integrating the template to the demo app. Let's create another Home template under master template.

Step 04: Create a Document Type "Home" 

Go to the Umbrao back-office and navigate to the Content Type node. Create Home and Choose the template as Home as shown below.


Step 05: Create a Home Page 

Navigate to the Content Node and Create home page. Make sure the template is set to Home and click "Save and publish".

That's it. Give a hit to http://localhost/UmbracoDemoApp/ to view the integrated template in UmbracoDemoApp. In the next article will see how to create partial views for the header, footer, and sidebar. Refer Create a Master Template(layout) in Umbraco CMS part 2 for more details.

Monday, January 14, 2019

Install Umbraco on Localhost and Build Visual Studio Solution

Let's create a visual studio project and install Umbraco in the solution.

Note:You need to run Visual studio as Administrator to setup any website in local IIS.

Step 01: Create a Visual studio project

You will be prompt to select the web application type (.Net core/ .Net framework). Make sure you create the Umbraco application based on .Net framework (I have selected 4.6.1 version). Give a proper name for your project solution (I just named it as UmbracoDemoApp).


Step 02: Select an empty project

Do not select the project type as MVC or Add folders and core references for MVC. Umbraco will overwrite your application web.config and will generate Global.asax file for you. In this demo, I will not add an unit test project for this demo, feel free to drive the car as you wish.


Step 03: Get ready to install UmbracoCMS
 
Open up the package Manager Console to install UmbracoCMS in command line. If you wish to use, NuGet package manager, right click on the project solution and go to Nuget search explore window, then search for the UmbracoCMS.


Step 04: Install UmbracoCMS

In this demo application I am going to use 7.12 version of UmbracoCMS. I prefer to go with 7.12 over 7.13 because most of the packages are not fully tested on 7.13 version.

PM> Install-Package UmrbacoCMS -version 7.12


Once the packages successfully installed on your machine, You can see the below screen in your project solution. Let's host the application in local IIS. Make sure you have opened the visual studio as Administrator.


Step 05: Host Demo application in Local IIS

Right click on project solution name --> Go to properties --> select Web tab --> Choose Local IIS --> click Create Virtual Directorty--> Run your application.

It will open the browser in following url:
http://localhost/UmbracoDemoApp

If you wish to change the hosting url like www.myfirstumbracoapp.com in your local machine before going live, refer my article on How to change the host url in Host file on Windows

During the installation process, it will prompt you to enter default settings for your application. Provide necessary information as you can see in below images.


Configure the database for your application.

Note: It is a good practice to create a different user account and a password for the database.You must create a database first to provide in following screen.


Once you complete the setting, click continue. You will be prompt to another screen to Install a starter website. Click "No thanks, I do not want to install a starter website". We are going to integrate bootstrap theme to the site in upcoming article.

Friday, January 11, 2019

Getting started with Umbraco CMS 7+

Introduction
This is going to be a complete Umbraco developer training series for any developer who has experienced on ASP.Net MVC framework.

In simpler language, a content management system handles all that basic infrastructure for a website features like creating web pages, storing images, include custom layouts and other functions for you so that you can focus on more forward-facing parts of your website.

In this series I am planning to cover following topics which require basic knowledge of day to day C# programming in MVC.

Note:
Throughout the series, I am going to re-use the the same source code from initial step. Also, a free Bootstrap responsive template will be integrated to the Master layout to make the site more user friendly and attractive ;)

  1. Getting started with Umbraco CMS 7+
  2. Install Umbraco on Localhost and Build Visual Studio Solution
  3. Create a Master Template(layout) in Umbraco CMS part 1
  4. Create a Master Template(layout) in Umbraco CMS part 2