Developer 101 - Lab: Create a Featured Products Module

In this lab, you will be creating a Module that allows content editors to search and select from a list of available of “Products” and have them featured in a listing on the website. We will be making use of the existing Digital Content List found in Shared Content > Dynamic Page Content called “Products”. Dynamic Page Content is a special type of Shared Content that allows content lists to be used dynamically as content for a Page. Each Product has its own dynamic page and this is important, but not relevant to this exercise.

Again, you will need to have the Agility Content Manager open in a web browser and our Sample Site open in Visual Studio. At the end of the lab, you will have a list of featured “Products” and add this to the existing “home” page.

  1. Start by navigating to Settings > Module Definitions in Agility.

  2. Click New Module Definition.

  3. Enter “Featured Products” as the Name and ensure the Reference Name is auto-populated.

  4. Enter “Displays a list of Featured Products.” as the Description.

  5. Click Save to save this module.

  6. This module will have several properties including “Title”, “ProductsIDs”, and “Products”. Click on the Form Builder tab and click Add Field.

  7. Enter “Title” as the Field Label and ensure the Field Name is also filled out. Set the Property Type to “Text”. Click Save & New.

  8. Enter “Products IDs” as the Field Label and ensure the Field Name is also filled out. Set the Field Type to “Text” and mark the field as “Hidden Value” – this will hide the text field from the input form. We’ll need this later to store our selected and sorted Product IDs. Click Save & New.

  9. Enter “Products” as the Field Label and ensure the Field Name is also filled out. Set the Property Type to “Linked Content”. Additional fields relating to Linked Content will be shown.

  10. Set the Content Definition to “Product”.

  11. Set the Content View to “Shared Content”. This will show an additional field called Shared Content allowing you to select a Shared Content List that exists based on the “Product” content definition. Select “Products”.

  12. Set Render as “Search List Box”. Additional fields relating to the Search List Box will be displayed.

  13. Click the Choose Columns link and ensure “Title” is selected.

  14. Leave the Filter Expression field blank.

  15. Leave the Default Search Value field blank.

  16. Set the Save Value To Field to your hidden property you created called “Products IDs”.

  17. The Save Text To Field property can be left blank in this case, although can be useful when wanting to store the Title of the Product in addition to its ID.

  18. Click Save & Close.

  19. Click on the Output Template

  20. Select “MVC Controller Action” as the Output Type and enter “Products” as the Controller and “Featured” as the Action.

  21. Click Save & Close on the Edit Module Definition dialogue.

  22. Click Download API to download your latest C# classes and replace the contents of the existing API file Models/MVC4SampleSite_API.cs.

  23. In Agility navigate to Pages and click on the page labelled “home”. Add the “Featured Products” module to the Main Content Zone.

  24. Set the Title to “Featured Products”.

  25. Click Save to save the module and initialize the Linked Content property.

  26. Enter “processor” into the search field and hit Enter on your keyboard to search the Products list for any Product Title that contains “processor”.

  27. If you don’t receive any results, change the search term and try again. You can always click on the EDIT CONTENT button to see the entire Products list.

  28. Once you have some search results, click the autocomplete item to select the item. Note you can also select items from your Selection list and move them left/right to customize your sort order. Adding, removing, or sorting items will change the comma delimited list of IDs that are being stored in your hidden “Products IDs” field.

  29. Once you are satisfied with your selection click Save & Close to close the module.

  30. Next, we need to add in the appropriate Controller Action method and create our partial view for our module.

  31. In Visual Studio open the Controller called “ProductsController”. You’ll notice we already have other Controller Action Methods here for our general Product Listing and Product Details modules. Add a new ActionResult near the bottom called “Featured” and we will pass in the Module_FeaturedProducts class as a parameter called “module”.

      public ActionResult Featured(Module_FeaturedProducts module)
           return PartialView("~/Views/Products/FeaturedProducts.cshtml", module); 
  32. Before we move onto creating our PartialView called “FeaturedProducts.chsml”, let’s add some validation to this module. We only want to output this module if we have any products that were selected. If the editor didn’t select any products, or the products they selected have been unpublished or deleted then we don’t want to display something that looks like empty content on the website. To get around this, we can add some logic that only renders the partial view if we have the associated content.

    public ActionResult Featured(Module_FeaturedProducts module)
        //check if the Products linked content is initialized and the 'Products' list still exists, 
        //then get products by our selected IDs and see if we have any
        //if we do, then display then render the view, else return nothing
        if (module.Products != null && mod-ule.Products.GetItemsByIDs(module.ProductsIDs).Any())
            return PartialView("~/Views/Products/FeaturedProducts.cshtml", module); 
        return null;
  33. Create a new Partial View in Views/Products/ called “FeaturedProducts” and set the model of the view to Module_FeaturedProducts.

    @model MVC4SampleSite.Models.Module_FeaturedProducts
  34. Type in the following code into your view:

    @* Adding Agility.Web.Extensions allows us to use the method GetItemsByIDs which filters the content view by our IDs *@
    @using Agility.Web.Extensions
    @model MVC4SampleSite.Models.Module_FeaturedProducts
    <div class="featured-products">
        @* If the Title is empty, don't display anything *@
        @if(!string.IsNullOrEmpty(Model.Title)) {
        <div class="row">
            @foreach(var product in Model.Products.GetItemsByIDs(Model.ProductsIDs)) 
            <div class="col-md-4">
                <a href="@product.Url("~/products/product-details")">
                @if(product.ListingImage != null) { 
                <img src="@product.ListingImage.URL" class="img-responsive" />
  35. Build your project and Start Without Debugging in Visual Studio. On the homepage, you should see your Featured Products module along with any products you have selected.

0 out of 0 found this helpful



Please sign in to leave a comment.