Copied to clipboard

Flag this post as spam?

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


  • Lance Taylor 11 posts 83 karma points
    Jan 20, 2021 @ 19:40
    Lance Taylor
    0

    Content not updating on page when Saved.

    Hello all,

    So my question is likely fairly simple. I am updating content within a grid. Specifically a placeholder FullWidth section that is included by default. Where I am now is that after pulling in some data from an RSS feed and overwriting the grid content with it I am attempting to save it. When I save it it does not throw an error and says it worked but upon visiting the page none of the values have been updated.

    Here is the relevant code

            private void ContentService_Publishing(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.ContentPublishingEventArgs e)
        {
            //FOR EACH PAGE Get Grid bodyText Value
            // Get access to ContentService
    
            // Create a variable for the GUID of the page you want to update
            // GUID is found con info tab of content page in Umbraco Back Office
            Guid guid = Guid.Parse("9d0210ac-dec7-44a9-9d61-523d31474601");
    
            // Get the page using the GUID you've defined
            IContent content = _contentService.GetById(guid); // ID of your page
            content = _contentService.GetById(content.Id); // ID of your page
    
    
            // Get Page Value (Main, Sports, US, World, ECT) THIS IS IN JSON
            string jsonValue = GetCurrentContentValues(content);
            TopLevelContent topLevelContent = JsonConvert.DeserializeObject<TopLevelContent>(jsonValue);
            //// Use proper RSS feed to assign values to html value
            //// Create a variable for the desired value and write 
            HtmlString htmlValue = GetNewContentValueFromRSS(topLevelContent);
    
            //// Overwrite all values in the current JSON object
            WriteValuesToContent(content, htmlValue);
    
            // Save and Publish each page (Content)
            //if (previousPageId != content.Id)
            //{
            //previousPageId = content.Id;
            _contentService.Save(content, -1, false);
            //_contentService.SaveAndPublish(content, "*", -1, false);
            //}
    
        public void WriteValuesToContent(IContent content, HtmlString htmlValue)
        {
            // Set the value of the property with alias 'bodyText'. 
            content.SetValue("bodyText", htmlValue);
        }
    
    
        }
    
  • Mario Lopez 168 posts 952 karma points MVP 3x c-trib
    Jan 20, 2021 @ 22:10
    Mario Lopez
    0

    What structure has that JSON you are trying to save? The Grid needs its specific JSON object to work.

    You could try using GridData to get a strongly typed representation of your grid.

  • Lance Taylor 11 posts 83 karma points
    Jan 20, 2021 @ 22:14
    Lance Taylor
    0

    I built a custom serializer that overwrites the values that are to be replaced while retaining the overall structure. I pull the JSON from the bodyText which is the Grid alias. This is then deserialized into C# objects where the placeholder values are overwritten by the RSS feed. This is then reserialized and saved.

    I have confirmed the deserializer and reserialization process works providing the correct structure.

Please Sign in or register to post replies

Write your reply to:

Draft