Copied to clipboard

Flag this post as spam?

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


  • Mike Poole 53 posts 165 karma points
    Apr 24, 2018 @ 15:09
    Mike Poole
    0

    Publishing content to a JSON file

    We have a requirement that when our site is published, the client wishes to have certain nodes published to a JSON file so that their app that compliments the website can read the data.

    The nodes in question are simple ones (document with several children)

    To create the data in a JSON format, how could I create a publish event to do this?

    Thanks

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Apr 24, 2018 @ 15:52
    Steve Morgan
    1

    Hi Mike,

    This should be easy enough to do.

    You need to have a custom publish event, check if the node is a doc type you're interested in and then call a helper utility to write out the JSON.

    Subscribing to events is covered here: https://our.umbraco.org/Documentation/Getting-Started/Code/Subscribing-To-Events/ https://umbraco.tv/videos/umbraco-v7/developer/extending/events/

    I'd use the UmbracoHelper to get the content in a structure and then writing out as a json file should be a trivial job.

    HTH

    Steve

  • Mike Poole 53 posts 165 karma points
    Apr 27, 2018 @ 11:27
    Mike Poole
    0

    Thanks Steve

    I've got the listener working thanks to your post

    With regards to the getting the content in a structure, I now have IContent of my node - how does the UmbracoHelper allow me to get it in a structure I want (only certain properties) to then serialize into JSON?

    Mike

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Apr 27, 2018 @ 12:32
    Steve Morgan
    0

    Are you writing out a number of nodes or just the one that's changed?

    If you're just doing the current node then use the IContent - I would create a custom model to represent your json so you can create the object (or list / array of objects) and then simply write this out as JSON.

    If you have a lot of nodes you have to query and then write back out then you'll want to use the UmbracoHelper so you're using the cache. If it's just a few nodes then you might just be able to use the ContentService - just beware this hits the DB.

    Either way - be careful if you use the cache, it might not have been updated on the publish even if you hook into the wrong event!!

  • Mike Poole 53 posts 165 karma points
    Apr 30, 2018 @ 13:06
    Mike Poole
    0

    Thanks Steve, that is really helpful

    I am trying to write out multiple nodes (when a node of type "FAQ" is saved, I want to write out ALL the FAQ nodes to a JSON file

    So to use the UmbracoHelper to do so, how would I amend the code you kindly supplied below?

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Apr 27, 2018 @ 12:52
    Steve Morgan
    0

    To get the property values from IContent use something like:

    namespace My.Namespace
    {
        public class RegisterUmbracoEvents : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentServicePublished;
            }
    
            private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                TestImportProperty myJsonObject = new TestImportProperty();
    
                foreach (var node in args.PublishedEntities)
                {
                    if (node.ContentType.Alias == "nodeDocTypeAliasGoesHere")
                    {
                        if (node.HasProperty("someProperty"))
                        {
                           // get each property from the node to match your json object
                            myJsonObject.SomeProperty = node.GetValue<string>("someProperty");
                        }
    
                        string json = JsonConvert.SerializeObject(myJsonObject);
    
                        //write string to file
                        System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("~") + "\\App_Data\\json\\json.txt", json);
                    }
                }
            }
        }
    }
    

    NOTE - the example above would overwrite the file for each published node- it's likely you'll want to build a list and write this out - here serialise the list of "json" objects (probably have to covert it to an array first).

  • 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