How to create a record with blocks through content management api

I have a model that uses some block content, in the following query the distributorDetail, topTiles, middleTiles and bottomTiles are blocks. I am having issues creating new records through the content management api, I don’t see any examples of it adding new blocks in documentation. …
query MyQuery {
portalTileCollection {
title
distributorDetail {
distributorId
}
topTiles {
id
title
fontAwesomeTileCode
tileUrl
titleColorHexCode
}
middleTiles {
id
title
fontAwesomeTileCode
tileUrl
titleColorHexCode
}
bottomTiles {
id
title
fontAwesomeTileCode
tileUrl
titleColorHexCode
}
}
}
I am using c# backend to send payload to content management api to add new record for the above model.
Here is example payload I send:

"{"data":{"type":"item","attributes":{"title":"Test","distributor_detail":null,"top_tiles":[{"title":"eBiz","font_awesome_tile_code":"fas fa-shopping-cart ","tile_url":"https://test.com/Login/Identity?PortalId=sadmin ","title_color_hex_code":" #ffffff"},{"title":" eService","font_awesome_tile_code":"fas fa-tools","tile_url":"https://test.com","title_color_hex_code":"#ffffff"},{"title":" iCademy Test","font_awesome_tile_code":"fas fa-graduation-cap","tile_url":"https://test.com ","title_color_hex_code":" #ffffff "},{"title":"  Parts Lookup","font_awesome_tile_code":"fas fa-tools","tile_url":"https://test.com/#/?login=us","title_color_hex_code":"#ffffff"},{"title":" Dealer Support","font_awesome_tile_code":"fas fa-info-circle","tile_url":"https://test.com/","title_color_hex_code":"#ffffff"},{"title":"  Documents","font_awesome_tile_code":"fas fa-file","tile_url":"https://test.com/technical-document-groups","title_color_hex_code":"#ffffff"},{"title":"OUTFITTERS","font_awesome_tile_code":"fas fa-info-circle","tile_url":"https://www.test.com","title_color_hex_code":"#ffffff"},{"title":" Marketing Library","font_awesome_tile_code":"fas fa-image","tile_url":"https://test.com/SMProxy/Link","title_color_hex_code":"#ffffff"},{"title":"Reserved","font_awesome_tile_code":"fas fa-info-circle","tile_url":"","title_color_hex_code":"#ffffff"}],"middle_tiles":[{"title":"Bid Award Assistance","font_awesome_tile_code":"fas fa-shopping-cart ","tile_url":"https://test.com/","title_color_hex_code":"#ffffff"},{"title":" ProFleet","font_awesome_tile_code":"fas fa-shopping-cart","tile_url":"https://profleet.test.com/","title_color_hex_code":"#ffffff"},{"title":" Booking","font_awesome_tile_code":"fas fa-shopping-cart","tile_url":"https://booking.test.com/","title_color_hex_code":"#ffffff"}],"bottom_tiles":[{"title":"Analytics","font_awesome_tile_code":"fas fa-info-circle","tile_url":"https://test.net/","title_color_hex_code":"#ffffff"},{"title":" Reserved","font_awesome_tile_code":"fas fa-info-circle","tile_url":"","title_color_hex_code":"#ffffff"},{"title":" Reserved","font_awesome_tile_code":"fas fa-info-circle","tile_url":"","title_color_hex_code":"#ffffff"}]},"relationships":{"item_type":{"data":{"type":"item_type","id":"2047003"}}}}}"

An example error I get back is

"{"data":[{"id":"11f0fb","type":"api_error","attributes":{"code":"INVALID_FIELD","details":{"field":"bottom_tiles.0","code":"INVALID_FORMAT","messages":["#: failed schema #: \"font_awesome_tile_code\", \"tile_url\", \"title\", \"title_color_hex_code\" are not permitted keys.","#: failed schema #: \"attributes\", \"relationships\", \"type\" weren't supplied."]}}}]}"

Anyone see what I’m doing wrong?

Should I just make it more simple and not use blocks and maybe use JSON fields instead? If so, are there any examples of adding records with JSON fields I can look at?

Thank you.

Hello @jterry

The objects representing the blocks inside the array in your request should be instead structured like this (changing the ID for your block model ID, and adding the attributes you want inside, title, font_awesome_tile_code, …)

[
  {
    "type": "item",
    "attributes": {
      // ... put your fields values here
    },
    "relationships": {
      "item_type": {
        "data": {
          // the block model
          "id": "435822",
          "type": "item_type",
        }
      }
    },
  },
  // ...
]

Hi, thanks. So what if I want to add all the blocks for one record in one api call, is that possible? Can the attributes be an array of objects?

Or maybe you mean like the below instead?

{
  "data": {
    "type": "item",
    "attributes": {
      "title": "Test",
      "distributor_detail": null,
      "top_tiles": [
        {
          "type": "item",
          "attributes": {
            "title": "eBiz",
            "font_awesome_tile_code": "fas fa-shopping-cart ",
            "tile_url": "https://test.com/Login/Identity?PortalId=te ",
            "title_color_hex_code": " #ffffff"
          },
          "relationships": {
            "item_type": {
              "data": {
                "id": "435822",
                "type": "item_type"
              }
            }
          }
        },
        {
          "type": "item",
          "attributes": {
            "title": "eBiz2",
            "font_awesome_tile_code": "fas fa-shopping-cart2 ",
            "tile_url": "https://test.com/Login/Identity?PortalId=ten ",
            "title_color_hex_code": " #ffffff"
          },
          "relationships": {
            "item_type": {
              "data": {
                "id": "435822",
                "type": "item_type"
              }
            }
          }
        }
      ]
    },
    "relationships": {
      "item_type": {
        "data": {
          "type": "item_type",
          "id": "2047003"
        }
      }
    }
  }
}

Edit: I just tested the above and it seems to work, thanks for pointing me in the right direction.

1 Like

What would a multilingual block look like here?
Still struggling to figure this out with the current documentation.

Hello @ldc

Each locale would have an array with its own block objects

so

modular_content_field: {
  en:  [...],
  it: [...],
  es: [...]
}
1 Like