Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 14:43
    Christine
    1

    Coding Help: Adding multiple document types in news overview

    I'm looking for some coding help (i'm not a developer)

    I need to add some document types to the News Overview so that it will show up on the blog page.

    The News Overview is looking for all the umbNewsItem document types, which is what I want, but I need to add some more document types:

    familyNutritionItem, newComersNutritionItem, nutritionItem

    Here is the code. Thanks in advance!

    @inherits UmbracoTemplatePage
    @{
        Layout = "umbMaster.cshtml";
    
        int pageSize = 8; // How many items per page
        int page;          // The page we are viewing
    
        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }
    
        // If the editor has not explicitly provided the "Page title" property page 
        // then just show the name of the page otherwise show the provided title
        var pageTitle = string.IsNullOrWhiteSpace(CurrentPage.Title)
            ? CurrentPage.Name
            : CurrentPage.Title;
    
        // Model.Content is the current page that we're on
        // AncestorsOrSelf is all of the ancestors this page has in the tree
        // (1) means: go up to level 1 and stop looking for more ancestors when you get there
        // First() gets the first ancestor found (the home page, on level 1)
        var homePage = CurrentPage.AncestorsOrSelf(1).First();    
    
        // Find all pages with document type alias umbNewsOverview
        // We do that using the plural, umbNewsOverviews (note the extra "s" in the end)
        // Then take the first one, as we know there will only be on news overview page
        var newsOverview = homePage.umbNewsOverviews.First();
    
        // Similar to above: find all pages with document type umbNewsItem under the news overview page
        // Then order them, first by publishDate (a property the editor can explicitly set on the news item)
        // and then by createDate, which is set by Umbraco automatically when a page gets created.
        var newsItems = newsOverview.umbNewsItems.OrderBy("publishDate desc, createDate desc");
    
        int totalItems = newsItems.Count();
        int totalPages = (int)Math.Ceiling((double)totalItems / (double)pageSize);
    
        if (page > totalPages)
        {
            page = totalPages;  
        }
        else if (page < 1)
        {
            page = 1;
        }
    }
    
    @{ Html.RenderPartial("umbInsideBanner"); }
    <!-- Main -->
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l9">
                <div class="row">
                    <article>
                        @foreach (var item in newsItems.Skip((page - 1) * pageSize).Take(pageSize))
                        {
    
                            // If the editor has not explicitly provided the "Page title" property page 
                            // then just show the name of the page otherwise show the provided title
                            var title = string.IsNullOrWhiteSpace(item.Title) 
                                ? item.Name 
                                : item.Title;
    
    
                            // If the editor has not explicitly set the publishDate property then show the create date
                            var dateTime = item.PublishDate == default(DateTime) 
                                ? item.CreateDate 
                                : item.PublishDate;
    
                            <div class="col s12">
                                <div class="card">
                                    <div class="card-content">
                                        <section>
                                            <h4><a href="@item.Url">@title</a></h4>
                                            <span>@item.SubHeader</span>
                                            <ul>
                                                <li class="blog-timestamp">@dateTime.ToString("f")</li>
                                            </ul>
    
                                            @if (string.IsNullOrWhiteSpace(item.BlogImage) == false)
                                            {
                                                <a href="@item.Url" class=""><img src="@item.Image" alt="" /></a>
                                            }
    
    
                                            @Umbraco.Truncate(item.BlogContent, 100)
                                             <div class="card-action">
                                                <a href="@item.Url">Continue Reading...</a>
                                            </div>
                                        </section>
                                    </div>
                                </div>
                            </div>
    
                        }
                    </article>
                </div>
            </div>
            <div class="col s12 m12 l3">
                <!--<nav class="white">
                    <div class="nav-wrapper">
                        <form method="get" action="../search-results">
                            <div class="input-field">
                                <input type="search" id="keywords" placeholder="" name="keywords" value="" /> 
                                 <label class="label-icon" for="keywords"><i class="material-icons">search</i></label>
                                <i class="material-icons">close</i>
                             </div>
                        </form>
                    </div>
                </nav> -->
                <div class="card">
                    <div class="card-content">
                        <span class="card-title card-title-newsletter">Subscribe and stay updated with our FREE newsletter.</span>
                        @Umbraco.RenderMacro("NewsletterForm")
                    </div>
                </div>
                <div class="card">
                    <div class="card-content">
                        <a class="twitter-timeline" data-height="560" href="https://twitter.com/MenuSano">Tweets by MenuSano</a> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
                    </div>
                </div>
                <div class="card">
                    <div class="card-content">
                        @{ Html.RenderPartial("umbLatestNewsWidget"); }
                    </div>
                </div>
            </div>
          </div>    
        <div class="row">
            <div class="col s12">
                @for (int p = 1; p < totalPages + 1; p++)
                {
                    if (p == page)
                    {
                        <span style="font-size:large;padding-right:10px">@p</span>
                    }
                    else
                    {
                        <a href="?page=@p" title="Go to page @p of results" style="font-size:large;padding-right:10px">@p</a>
                    }
                }
            </div>
        </div>
    
    </div>
    <div class="parallax-container">
            <div class="parallax">
               @{
                if (CurrentPage.HasValue("bottomBannerImage"))
                    {
                        var dynamicMediaItem = Umbraco.Media(CurrentPage.bottomBannerImage);
                        <img src="@dynamicMediaItem.Url" style="display: block; transform: translate3d(-50%, 381px, 0px);" alt="@CurrentPage.altText" />
                    }
                }
            </div>
        </div>
    <!-- /Main -->
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 15:09
    Alex Skrypnyk
    0

    Hi Christine

    Try this code, please:

    var newsItems = newsOverview.Descendants().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1 || NodeTypeAlias = @2 || NodeTypeAlias = @3", "familyNutritionItem", "newComersNutritionItem", "nutritionItem", "umbNewsItem").OrderBy("publishDate desc, createDate desc");
    

    Hope it will work.

    Alex

  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 15:18
    Christine
    0

    Hi Alex,

    Thanks for this. It only grabbed the first one "familyNutritionItem"

    I think some double "=" signs were missing so I added them:

    var newsItems = newsOverview.Descendants().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1 || NodeTypeAlias == @2 || NodeTypeAlias == @3", "familyNutritionItem", "newComersNutritionItem", "nutritionItem", "umbNewsItem").OrderBy("publishDate desc, createDate desc");
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 15:17
    Alex Skrypnyk
    0

    But be aware that dynamics are not best practice for Umbraco. Christine, what version of Umbraco are you using?

  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 15:20
    Christine
    0

    Oh really? This is the only way I know how to make a blog. This site's version is 7.5.7

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 15:24
    Alex Skrypnyk
    0

    try please this one:

    var newsItems = newsOverview.DescendantsOrSelf().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1 || NodeTypeAlias = @2 || NodeTypeAlias = @3", "familyNutritionItem", "newComersNutritionItem", "nutritionItem", "umbNewsItem").OrderBy("publishDate desc, createDate desc");
    

    can you show a screen of your structure?

  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 15:35
    Christine
    0

    Thanks again! It is only picking up the first item "familyNutritionItem"

    I did add the double = to @2 & @3

    Is this the structure you meant?

    enter image description here

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 15:37
    Alex Skrypnyk
    0

    Christine, the structure of your content pages.

  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 15:40
    Christine
    0

    enter image description here

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 16:22
    Alex Skrypnyk
    0

    Try this code, please:

    @inherits UmbracoTemplatePage
    @{
        Layout = "umbMaster.cshtml";
    
        int pageSize = 8; // How many items per page
        int page;          // The page we are viewing
    
        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }
    
        // If the editor has not explicitly provided the "Page title" property page 
        // then just show the name of the page otherwise show the provided title
        var pageTitle = string.IsNullOrWhiteSpace(CurrentPage.Title)
            ? CurrentPage.Name
            : CurrentPage.Title;
    
        // Model.Content is the current page that we're on
        // AncestorsOrSelf is all of the ancestors this page has in the tree
        // (1) means: go up to level 1 and stop looking for more ancestors when you get there
        // First() gets the first ancestor found (the home page, on level 1)
        var homePage = Umbraco.AssignedContentItem.AncestorsOrSelf(1).First();    
    
        // Find all pages with document type alias umbNewsOverview
        // We do that using the plural, umbNewsOverviews (note the extra "s" in the end)
        // Then take the first one, as we know there will only be on news overview page
        var newsOverview = homePage.Children.First(x => x.DocumentTypeAlias.Equals("umbNewsOverviews")));
    
        // Similar to above: find all pages with document type umbNewsItem under the news overview page
        // Then order them, first by publishDate (a property the editor can explicitly set on the news item)
        // and then by createDate, which is set by Umbraco automatically when a page gets created.
        var newsItems = newsOverview.DescendantsOrSelf()
            .Where(x => x.DocumentTypeAlias.Equals("familyNutritionItem") || x.DocumentTypeAlias.Equals("newComersNutritionItem") || x.DocumentTypeAlias.Equals("nutritionItem") || x.DocumentTypeAlias.Equals("umbNewsItem"))
            .OrderByDescending(x => x.GetPropertyValue<DateTime>("publishDate"))
            .ThenByDescending(x => x.CreateDate);
    
        int totalItems = newsItems.Count();
        int totalPages = (int)Math.Ceiling((double)totalItems / (double)pageSize);
    
        if (page > totalPages)
        {
            page = totalPages;  
        }
        else if (page < 1)
        {
            page = 1;
        }
    }
    
    @{ Html.RenderPartial("umbInsideBanner"); }
    <!-- Main -->
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l9">
                <div class="row">
                    <article>
                        @foreach (var item in newsItems.Skip((page - 1) * pageSize).Take(pageSize))
                        {
    
                            // If the editor has not explicitly provided the "Page title" property page 
                            // then just show the name of the page otherwise show the provided title
                            var title = string.IsNullOrWhiteSpace(item.GetPropertyValue<string>("Title")) 
                                ? item.Name 
                                : item.GetPropertyValue<string>("Title");
    
    
                            // If the editor has not explicitly set the publishDate property then show the create date
                            var dateTime = item.GetPropertyValue<DateTime>("publishDate") == default(DateTime) 
                                ? item.CreateDate 
                                : item.GetPropertyValue<DateTime>("publishDate");
    
                            <div class="col s12">
                                <div class="card">
                                    <div class="card-content">
                                        <section>
                                            <h4><a href="@item.Url">@title</a></h4>
                                            <span>@item.GetPropertyValue("subHeader")</span>
                                            <ul>
                                                <li class="blog-timestamp">@dateTime.ToString("f")</li>
                                            </ul>
    
                                            @if (string.IsNullOrWhiteSpace(item.GetPropertyValue<string>("blogImage")) == false)
                                            {
                                                <a href="@item.Url" class=""><img src="@item.GetPropertyValue("image")" alt="" /></a>
                                            }
    
    
                                            @Umbraco.Truncate(item.GetPropertyValue<string>("blogContent"), 100)
    
                                            <div class="card-action">
                                                <a href="@item.Url">Continue Reading...</a>
                                            </div>
                                        </section>
                                    </div>
                                </div>
                            </div>
    
                        }
                    </article>
                </div>
            </div>
            <div class="col s12 m12 l3">
                <!--<nav class="white">
                    <div class="nav-wrapper">
                        <form method="get" action="../search-results">
                            <div class="input-field">
                                <input type="search" id="keywords" placeholder="" name="keywords" value="" /> 
                                <label class="label-icon" for="keywords"><i class="material-icons">search</i></label>
                                <i class="material-icons">close</i>
                            </div>
                        </form>
                    </div>
                </nav> -->
                <div class="card">
                    <div class="card-content">
                        <span class="card-title card-title-newsletter">Subscribe and stay updated with our FREE newsletter.</span>
                        @Umbraco.RenderMacro("NewsletterForm")
                    </div>
                </div>
                <div class="card">
                    <div class="card-content">
                        <a class="twitter-timeline" data-height="560" href="https://twitter.com/MenuSano">Tweets by MenuSano</a> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
                    </div>
                </div>
                <div class="card">
                    <div class="card-content">
                        @{ Html.RenderPartial("umbLatestNewsWidget"); }
                    </div>
                </div>
            </div>
        </div>    
        <div class="row">
            <div class="col s12">
                @for (int p = 1; p < totalPages + 1; p++)
                {
                    if (p == page)
                    {
                        <span style="font-size:large;padding-right:10px">@p</span>
                    }
                    else
                    {
                        <a href="?page=@p" title="Go to page @p of results" style="font-size:large;padding-right:10px">@p</a>
                    }
                }
            </div>
        </div>
    
    </div>
    <div class="parallax-container">
        <div class="parallax">
            @{
                if (CurrentPage.HasValue("bottomBannerImage"))
                {
                    var dynamicMediaItem = Umbraco.Media(CurrentPage.bottomBannerImage);
                    <img src="@dynamicMediaItem.Url" style="display: block; transform: translate3d(-50%, 381px, 0px);" alt="@CurrentPage.altText" />
                }
            }
        </div>
    </div>
    <!-- /Main -->
    
  • Christine 115 posts 288 karma points
    Jun 01, 2017 @ 18:18
    Christine
    1

    I got the following error:

    Server Error in '/' Application.
    
    Compilation Error
    
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS1002: ; expected
    
    Source Error:
    
    
    Line 26:     // We do that using the plural, umbNewsOverviews (note the extra "s" in the end)
    Line 27:     // Then take the first one, as we know there will only be on news overview page
    Line 28:     var newsOverview = homePage.Children.First(x => x.DocumentTypeAlias.Equals("umbNewsOverviews")));
    Line 29: 
    Line 30:     // Similar to above: find all pages with document type umbNewsItem under the news overview page
    
    Source File: c:\Builds\Umbraco\Umbraco-ms\Views\umbNewsOverview.cshtml    Line: 28 
    
    
    Show Detailed Compiler Output:
    
    Show Complete Compilation Source:
    
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 01, 2017 @ 20:18
    Alex Skrypnyk
    0

    Hi Christine

    Ouh, sorry, just remove extra parenthesis at line 28:

    var newsOverview = homePage.Children.First(x => x.DocumentTypeAlias.Equals("umbNewsOverviews"));
    

    Thanks,

    Alex

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 04, 2017 @ 21:33
    Alex Skrypnyk
    0

    Hi Christine

    Did you solve the issue?

    Alex

  • Christine 115 posts 288 karma points
    Jun 28, 2017 @ 20:35
    Christine
    0

    Thanks for your help Alex, I was pulled to another project, so I can't say.

Please Sign in or register to post replies

Write your reply to:

Draft