Developer forum

Forum » Development » Creating GridRows via the Management API

Creating GridRows via the Management API

Justin Sjouw Dynamicweb Employee
Justin Sjouw
Reply

Hi, 

I'm trying to create a Page, With GridRows and Paragraphs via the management API. Could it be that the GridRowCreate only works correctyly in a visualeditor context? 

That is what my AI helper suggests per the analyses below:

GridRowCreate Management API — Non-functional Outside Visual Editor
 
## Summary
 
The `POST /admin/api/GridRowCreate` endpoint (command `Dynamicweb.Content.UI.Commands.VisualEditor.GridRowCreateCommand`) **always fails with HTTP 500** when called via the Management API. Two distinct bugs prevent it from working:
 
| # | Error | Trigger | Cause |
|---|-------|---------|-------|
| **Bug 1** | `ArgumentException: String value cannot be empty` | `GridId` not provided or incorrect | The command does not read `GridId` from the request payload; it tries to resolve it from the page's Layout object, which returns empty |
| **Bug 2** | `ArgumentOutOfRangeException: Index must be within the bounds of the List` | `GridId` correctly set to `"Page"` | The command tries to `List.Insert()` into the Visual Editor's in-memory row collection, which does not exist in the API context |
 
The endpoint is documented in the OpenAPI spec (`/admin/api/api.json`) and accepts all the correct parameters, but the underlying `GridRowCreateCommand` is a Visual Editor command that depends on server-side session state unavailable via the Management API.
 
## Environment
 
- **DynamicWeb 10** with **Swift 2.1** (Update 2.2)
- Tested on **two independent instances** — both fail identically
- Reproducible on **any page** — with or without existing grid rows
- Grid definition files are present on disk (`/Files/Templates/Designs/Swift-v2/Grid/Page/RowDefinitions/*.json`)
 
## API Spec (from `/admin/api/api.json`)
 
The `GridRowCreate` endpoint is documented with the following request model:
 
```json
{
  "PageId":      0,          // integer — target page ID
  "GridId":      "",         // string  — grid identifier within the layout
  "DefinitionId": "",        // string  — row definition (e.g. "1Column")
  "TemplateId":  0,          // integer — optional row template ID
  "Container":   "",         // string  — optional container reference
  "SortIndex":   0           // integer — insertion position
}

Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
I checked the implementation, and GridRowCreate does not appear to be strictly tied to a Visual Editor session.
What the code actually does in GridRowCreateCommand is:
read PageId, GridId, DefinitionId, Container and SortIndex
resolve the row definition from Layout + GridId
save the row
insert it into the page row list at SortIndex - 1
So the main problems look to be these:
1. GridId is not validated before use
If GridId is empty/invalid, GridRowDefinition.GetGridRowDefinition(layout, GridId, DefinitionId) can throw before the command returns a friendly error.
2. SortIndex is not validated
The command does: containerRows.Insert(row.Sort - 1, row);

so if SortIndex = 0 (or otherwise outside 1..count+1) it will throw ArgumentOutOfRangeException.
That means the endpoint is probably not broken because it requires Visual Editor context, but because it assumes the caller sends the same values the Visual Editor sends.
So for API usage, the request likely needs to be something like:

 

{
  "PageId": 123,
  "GridId": "Page",
  "DefinitionId": "1Column",
  "Container": "",
  "SortIndex": 1
}

 

In other words:
GridId must be the actual grid id from the layout
SortIndex must be 1-based, not 0
 
And below the AI analysis:
Investigation result:
The forum analysis is only partially correct. The code does not show GridRowCreate as inherently visual-editor-only. It shows two API robustness issues.
Findings:
GridRowCreateCommand has a real GridId property and uses it directly in GridRowDefinition.GetGridRowDefinition(layout, GridId, DefinitionId) in Dynamicweb.Content.UI\Commands\VisualEditor\GridRowCreateCommand.cs:18,98.
The visual editor client also sends GridId, Container, and SortIndex explicitly in Dynamicweb.CoreUI.Rendering\Client\src\js\Components\VisualEditor.js:145-147.
So the claim that the command “does not read GridId from the request payload” does not match the code.
Likely failure modes:
1. Missing or empty GridId causes HTTP 500
GridRowDefinition.GetGridRowDefinition(layout, gridId, definitionId) requires non-empty GridId and throws via Ensure.Argument.NotNullOrEmpty(gridId) in src\Core\Dynamicweb.Core\Rendering\Designer\GridRowDefinition.cs:116-123.
GridRowCreateCommand does not validate GridId first, so bad input becomes an exception instead of a clean 400.
2. SortIndex can cause ArgumentOutOfRangeException
The command does containerRows.Insert(row.Sort - 1, row) in Dynamicweb.Content.UI\Commands\VisualEditor\GridRowCreateCommand.cs:152-157.
SortIndex has no [MinimumValue(1)], unlike PageId.
If API callers use SortIndex = 0 from the sample/default payload, insert index becomes -1 and fails.
This is a stronger explanation than “missing visual editor session state”.

 

You must be logged in to post in the forum