Copied to clipboard

Flag this post as spam?

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


  • Carlos Mosqueda 244 posts 435 karma points
    Mar 07, 2022 @ 22:15
    Carlos Mosqueda
    0

    How to create a JSON feed for your Umbraco site directly in a View ... just because you can

    Not really a question, more of a "how-to" to be shared. I know going through the Umbraco API is probably the best way to do this, but in case you just wanted for "S and G's" want to make a JSON feed for your site or a section of you site, you can.... just because. Below is just one of many solutions, but here goes. Create a DocType and Template for the DocType and call them whatever you want. For this purpose I can name it "MyFeed.cshtml". I allow the doctype to be allowed at the root of my site. Create the page and then from there I just pull whatever nodes types I want into my feed. For this example, I am just pulling one type. Mainly for a post feed. I have this written in an Umbraco 7 site but I tried to update the code here for U8 and up. It may be a bit off.
    It is not a direct ".json" file extension, it is a URL, but without a layout tied to it, it outputs as text and can be parsed as JSON. Applications can be used for things such as apps or some minor headless implementations and can be extended.

    Let me know if it needs any editing. Just threw it up here.

     @*--GET YOUR SELF A LIMITED LIST OF NODES--*@
     @inherits Umbraco.Web.Mvc.UmbracoViewPage
     @{Layout = null;}
    @*JSON FEED GENERATOR in View*@
     @{
    var maxDisplayCount = 20; @*adjust as needed or edit code to leave out*@
    var items = new List<dynamic>();
      var treeParent = Umbraco.Content(2224); @*Whatever your node or just go Model.Root()*@
      var nodesList = treeParent.Descendants("blogPostPageDocType"); @*whatever you page type*@
     }
      @{
    
    if(nodesList != null && nodesList .Any()){
           foreach(var item in nodesList ){
                 var listImage = item.landingPageImage.Url;
                 var pageUrl = item.Url;
                 var baseUrl = "https://mysite.com";
    
                items.Add(new
                {
                   itemTitle = item.Name,
                   itemImageUrl = baseUrl + listImage,
                  itemUrl = item.Url
                });
            }
        }
    
    
        var trimmedItems = new List<dynamic>();
        if (maxDisplayCount > 0) {
            for (var x = 0; x < maxDisplayCount; x++) {
                trimmedItems.Add(items[x]);
            }
        }
    string json = Json.Encode(trimmedItems);
    Response.ContentType = "application/json";
    
     }
     @Html.Raw(json)
    
    
    
    
     @*---- OR GET YOURSELF ALL NODES---
       -- JSON DOES HAVE LIMITS SO BE AWARE OF LARGE PAYLOADS
      *@
    
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
    Layout = null;
    var items = new List<dynamic>();
    var treeParent = Umbraco.Content(2678); @*Whatever your node or just go Model.Root()*@
    var nodesList = treeParent.Descendants("blogPostPageDocType"); @*whatever you page type*@
    
    if(nodesList != null && nodesList .Any())
    {
        foreach(var item in nodesList )
        {
            var pageUrl = item.Url;
            var baseUrl = "https://mysite.com/";
            items.Add(new {itemTitle = item.Name, itemUrl = pageUrl});
        }
        }
    
        var trimmedItems = new List<dynamic>();
        for (var x = 0; x < items.Count(); x++) {
        trimmedItems.Add(items[x]);
        }
        string json = Json.Encode(trimmedItems);
        Response.ContentType = "application/json";
     }
    
     @Html.Raw(json)
    
Please Sign in or register to post replies

Write your reply to:

Draft