PatchAsync example

Hi, can you please explain how to partially update content using the .net client library?

            var accessory = new AccessoryData()
            {
                Name= objAccessoryImport.Name,
                Price = objAccessoryImport.Price,
            };

            var response = await accessoryClient.PatchAsync(accessory, XXX);

What do I need to put in the XXX in the line above?

Just have a look to the signature: https://github.com/Squidex/squidex-samples/blob/a42fa8ccddaa3fb8d8e874c6d059ded463721f90/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/SquidexClient.cs#L106

Have you got it working?

Not yet. I did see that documentation, but I’m not sure what a TPatch is.

Sorry, for the late response. TPatch is object of a class that represents your values you want to update. E.g. if you have three fields firstName, age and country and you only want to update country the squidex API expects the following JSON

{
  "age": {
     "iv": 99
  }
}

You have several options to implement that

Option 1: Create a custom update class

This approach works only with the custom JsonConverter that creates the nested object: https://github.com/Squidex/squidex-samples/blob/master/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/InvariantConverter.cs

public class AgeUpdate
{
   [JsonConverter(typeof(InvariantConverter))]
   public int Age { get; set; }
}

var update new AgeUpdate { Age = 99 };

Option 2: Create Json manually with JSON.NET

var update =
   new JObject(
     new JsonProperty("age", new JObject(
       new JsonProperty("iv", 99))); <-- The age

Option 3: Create an anonymous object

var update = new {
   age = new {
      iv = 123
   }
}

Option 4: Use dictionaries

var update = new Dictionary<string, object> {
    ["age"] = new Dictionary<string, int> {
        ["iv"] = 99
    }
}

As long as it can be serialized to the correct json you can choose whatever you want.

My recommendations is to use #1 or #3.

2 Likes

Awesome, thanks for the various solutions! This is exactly what I was after.