Copied to clipboard

Flag this post as spam?

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


  • Tim Gerhard 10 posts 125 karma points
    Sep 22, 2017 @ 07:59
    Tim Gerhard
    2

    Umbraco help for everyone <3

    Dear our.Umbraco.org Community I've been working with umbraco for several weeks now and I created a "cheat-sheet" with all the important stuff I've stumbled accros during developments. I'm a genuinely nice person - therefore I decided to share this file with you. Feel free to share, extend or improve my current Code. I'm still a beginner so don't expect too much. (Tested in Umbraco 7.6.5 and partially in 7.6.6)

    _____________
    

    Make a Redirect to Umbraco Login page on Startup for security reasons

    (Copy This code into Master.cshtml)

    @using Umbraco.Core.Security;
    
    var userTicket = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
        if (userTicket == null)
        {
            Response.Redirect("~/umbraco");
        }
    

    Get Image out of normal Media library

    bool hasImage = Model.Content.HasValue("image");
        var image = "";
        if (hasImage)
        {
            var mediaObject = Convert.ToString(Model.Content.GetPropertyValue("image"));
            int mediaIDimage = Convert.ToInt16(mediaObject);
            image = Umbraco.Media(mediaIDimage).Url;
        }
    
    
    If (hasImage) 
    {
    <img src=“@image“ …/>
    }
    

    Get Media out of Vorto Media library (in this case a pdf)

        IPublishedContent vorto = (IPublishedContent)Model.Content;
        bool hasPDF = Model.Content.HasVortoValue("pdf");
        var pdf = "";
        if (hasPDF) {
            var mediaItem = Convert.ToString(vorto.GetVortoValue<IPublishedContent>("pdf"));
            int mediaID = Convert.ToInt16(mediaItem);
            pdf = Umbraco.Media(mediaID).Url;
        }
    
    If (hasPDF) 
    {
    <a href=“@pdf“>PDF DOWNLOAD</a>
    }
    

    Get content out of a checkbox (in this case it's a checkbox which sets a css value

    string style = "";
        if (Convert.ToString(@Model.Content.GetPropertyValue("moveRight")) == "True")
        {
            style = "float:right;";
        }
    <a style="@style"></a>
    

    Basic Umbraco Navigation

    @{
        var root = CurrentPage.AncestorOrSelf(1);
        var rootSelection = root.Children.Where("Visible");
    }
    <div class="">
    @foreach (var page in rootSelection)
            {
                if (@page.Name != "Page Components")
                {
                    string isActive = "";
                    if (@page.Name == @Model.Content.Name)
                    {
                        isActive = "selected";
                    }
    
                    <div class="">
                        <a href="@page.Url" @isActive">@page.Name</a>
                    </div>
                }
            }
    </div>
    

    1x selection foreach

    @{ var selection = CurrentPage.Children.Where("Visible"); }
                    @if (selection.Any())
                    {
    
                        foreach (var item in selection)
                        {
                            if (@item.DocumentTypeAlias == "INSERT DOCUMENT TYPE ALIAS")
                            {
                                IPublishedContent ourModel = (IPublishedContent)item;
                                @Html.Partial("folder/INSERT PARTIAL NAME", ourModel)
                            }
                        }
                    }
    

    2x selection foreach (show everything but pageComponents)

    @{ var selection = CurrentPage.Children.Where("Visible"); }
    @if (selection.Any())
    {
        foreach (var item in selection)
        {
            if (@item.DocumentTypeAlias == "pageComponents")
            {
                var selection2 = (@item).Children.Where("Visible");
                foreach (var item2 in selection2)
                {
                    if (@item2.DocumentTypeAlias == "NAME OF COMPONENT")
                    {
                        IPublishedContent ourModel = (IPublishedContent)item2;
                        @Html.Partial("folder/PARTIAL VIEW NAME", ourModel)
    
                    }
                }
            }
        }
    }
    

    Get Content from certain Node

    int myNodeId = INSERT ID HERE;
        var myNode = Umbraco.TypedContent(myNodeId);
    
    @myNode.GetPropertyValue("PROPERTY NAME")
    

    Multiple Media picker values + Subfolders (if someone uploads a folder filled with images into the media picker)

                         @{
                                var typedMultiMediaPicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pictures");
                                foreach (var item in typedMultiMediaPicker)
                                {
    

    image if (item.DocumentTypeAlias == "Folder") { foreach (var image in item.Children("Image")) { image } } } }

    Value of Related Links with Vorto

    @using Newtonsoft.Json.Linq
    
    @{var itemSelection = item.GetVortoValue<JArray>("link");}
                                @if (itemSelection != null)
                                {
                                    foreach (var linkitem in itemSelection)
                                    {
                                        var linkUrl = (linkitem.Value<bool>("isInternal")) ? Umbraco.NiceUrl(linkitem.Value<int>("internal")) : linkitem.Value<string>("link");
                                        var linkTarget = linkitem.Value<bool>("newWindow") ? "_blank" : null;
                                        <a href="@linkUrl" target="@linkTarget">@(linkitem.Value<string>("caption"))</a>
                                    }
                                }     
    

    Umbraco Sub Navigation Concept

    This is a simple demonstration of how to make a simple subnavigation (dynamic)

    Step 1: Go to your Umbraco and create a Document Type „SubNavigation“ Step 2: Add a (vorto)TextString „ListName“ (or call it how you want). ListName ist the name that is displayed in the subnavigation. Step 3: Add this SubNavigation Document Type via Composition to every other tab. Step 4: Go to each DocumentType and add the Current ID in a hyper reference tag. Like this:

    <a href=“component-@Model.Id“></a>
    

    Step 5: Go to your navigation partial (or wherever you render your navigation) and make a 2x foreach selection (there is already a good example fort hat in this document) Step 6: Inside your 2x foreach loop you add this Code: IPublishedContent currentPage = Umbraco.TypedContent(@item2.Id);

    <a href="@page.Url#component-@item2.Id">@currentPage.GetVortoValue("listName")</a> 
    

    Info: I used a 2x loop because i my structure is like this:

    -Site  
      -PageComponents
           -Component 1
           -Component 2
           -...
    

    @item2 is the inner Selection oft he 2x foreach @page is the current Page you’re at. I used it for my main navigation.

    That's it :)

    Greetings - Tim

  • Rune Hem Strand 147 posts 911 karma points hq c-trib
    Sep 23, 2017 @ 19:44
    Rune Hem Strand
    0

    This is great Tim! So cool of you to share your tips & tricks!

    I would propose a small change; use Model.Content instead of CurrentPage. See the Developers Reference -> Common Pitfalls

    But well done and keep sharing!

    h5yr

    h5yr

    /rune

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies