Copied to clipboard

Flag this post as spam?

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


  • David Armitage 505 posts 2073 karma points
    Sep 05, 2017 @ 10:45
    David Armitage
    0

    Umbraco - Populate Grid Layout with rich text using the ContentService

    Hi Guys,

    Does anyone know how I can populate a grid layout using the content service.

    I have some html content I want to feel in there so I guess I need to firstly add a rich text editor to the grid view then add the html to the rich text editor.

    Cool but how do I do this in code???? Any ideas?

    public static void InsertBogArticle(ExternalBlogArticle blogArticle)
    {
                var contentService = ApplicationContext.Current.Services.ContentService;
                var name = blogArticle.Title;
                IPublishedContent blogArticlesFolder = BusinessManager.GetPublishedContentByDocumentTypeAlias("blogArticlesFolder");
                var data = contentService.CreateContent(name, blogArticlesFolder.Id, "blogArticle", 0);
    
                data.SetValue("mainHeading", blogArticle.Title);
                data.SetValue("summary", blogArticle.Summary);
                data.SetValue("description", blogArticle.Description);
                data.SetValue("date", blogArticle.Date);
                data.SetValue("tags", blogArticle.Tags);
                data.SetValue("isNews", blogArticle.IsNews);
    
                contentService.SaveAndPublishWithStatus(data);
    }
    

    Basically.

    data.SetValue("description", blogArticle.Description);
    

    "description" is a grid layout control

    blogArticle.Description is pure Html

    Thanks in advanced.

    Kind Regards

    David

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Sep 06, 2017 @ 08:27
    Kevin Jump
    1

    Short answer - yes - but it's not easy :(

    the grid is stored within umbraco as a big blob of JSON. if you look in your app_date/umbraco.config file you might see something that starts like...

    <![CDATA[{
      "name": "1 column layout",
      "sections": [
        {
          "grid": 12,
          "rows": [
            {
              "label": "Headline",
              "name": "Headline",
              "areas": [
                {
                  "grid": 12,
                  "hasConfig": false,
                  "controls": [
                    {
                      "value": "Headline things",
                      "editor": {
                        "alias": "headline"
                      },
                      "active": false
                    },
                    {
                      "value": "<p>Rich text withing a grid item.</p>",
                      "editor": {
                        "alias": "rte"
                      },
                      "active": false
                    }
                  ]
                }
              ],
              "hasConfig": false,
              "id": "07fdf021-7fe9-f524-74c1-f6f9f104ae5b"
            },
            {
              "label":
    

    The only real way to inject any extra content into this is to parse it and then find the right place and insert the values into it.

    if you are confident of all the settings you need to know then you can just insert blobs of json, but if you need flexability and to be able to know that you are putting the right type of content into the right place you also need to read the config of the grid (which contains all the definintions of things like headline, and quote etc...

    a call like this will load the grid config..

    gridConfig = UmbracoConfig.For.GridConfig(
                ApplicationContext.Current.ProfilingLogger.Logger,
                ApplicationContext.Current.ApplicationCache.RuntimeCache,
                new DirectoryInfo(HttpContext.Current.Server.MapPath(SystemDirectories.AppPlugins)),
                new DirectoryInfo(HttpContext.Current.Server.MapPath(SystemDirectories.Config)),
                HttpContext.Current.IsDebuggingEnabled);
    

    So it's not easy.

    As an example of grid manipulation: uSync has code to processes the grid when mapping the internal IDs https://github.com/KevinJump/uSync/blob/Dev-v7_4/Jumoo.uSync.Core/Mappers/GridMapper.cs the number of Debug lines is a good indication of how tricky it is.

Please Sign in or register to post replies

Write your reply to:

Draft