Adding Content Via API

I have created some new schema classes. Now I want to import some content to them. From my searches, it looks like I need to do this using the API. So, I have created a new Asp .Net project, added the squidex nuget package, and now I’m not exactly sure what I need to do. I haven’t found any good example code for this. Can someone point me in the right direction?

I’ve got this as my first step:

var clientManager = new SquidexClientManager(
https://cloud.squidex.io/”,
“xxx”,
“xxx:yyy”,
“myclientkey”);

With my own client keys above, and that seems to connect, now if I want to add content to a schema table called “models” which just has an int identifier called “ID”, and a string property called “Name”, how do I do this?

You are right, the documentation could be much better. But in the meantime you can have a look to the tests:

In short:

You have to define the classes first:

    public sealed class Model : SquidexEntityBase<ModelData>
    {
    }

    public sealed class ModelData
    {
        [JsonConverter(typeof(InvariantConverter))]
        public int Id { get; set; }

        [JsonConverter(typeof(InvariantConverter))]
        public string Name { get; set; }
    }

and then you get the client and use it to create the content:

var client = clientManager.GetClient<Model, ModelData>("models");

await client.CreateAsync(new ModelData ...);

Hi Sebastian,

Thanks for your prompt response and for providing sample code. I was getting the following error after following the example provided:

SquidexException: Squidex Request failed: {“message”:“Failed to create content.”,“details”:[“name: Not a known field.”,“id: Not a known field.”],“statusCode”:400}

I then went and changed the field names in the schema to lower case and then it was working properly. Are upper case characters in the field names not supported if you use the nuget package? Seemed to work fine when I tested the API calls with postman.

Anyway, I’ve got data going into the content which is the main thing. Thanks for your help.

PascalCase is converted to camelCase automatically. The assumption is that field names are managed in camelCase to be compatible with javascript and C# properties are in PascalCase.

But you can disable this behavior by annotation your data class with [KeepCasing]

Ok, thanks for the KeepCasing attribute. I’ve got some more question regarding references and many-to-many relationships, but I’ll start a new support thread. Cheers.

1 Like