Developer forum

Forum » Development » Quick and dirty Management API add product example

Quick and dirty Management API add product example

Justin Sjouw Dynamicweb Employee
Justin Sjouw
Reply

Hi,

I'm trying to create a (quick and dirty) example of how to add a product via the Management API.

I can add the product, and I add it to a Group with Category attributes attached to it. But I cannot get data into the category fields itself.

First I had 1 call to ProductSave including the category fields, which I have now spilt into 2 calls, thinking maybe the category fields only become availalble after the first save.

But even when first adding the product, getting back the new ID, and then executing a ProductSave again with the category fields, there is no data in the fields.

What am I missing?

these are the caetgory fields, 

and this is the code executing the ProductSave (twice)

// 1. Save product
            var productBody = new {
                Model = new {
                    Name = productName,
                    Number = productNumber,
                    GroupId = "GROUP362"
                }
            };
 
            var json = JsonSerializer.Serialize(productBody);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = client.PostAsync("/admin/api/ProductSave", content).Result;
 
            if (!response.IsSuccessStatusCode)
            {
                return $"❌ Product save error: {response.StatusCode} - {response.Content.ReadAsStringAsync().Result}";
            }
 
            var responseJson = response.Content.ReadAsStringAsync().Result;
            using var doc = JsonDocument.Parse(responseJson);
            var root = doc.RootElement;
 
            if (!root.TryGetProperty("model", out var modelElement) || !modelElement.TryGetProperty("id", out var productIdElement))
            {
                return "❌ Product saved but ProductId not found in response." + @responseJson;
            }
 
            var productId = productIdElement.GetString();
       
                       // 1. Update group category fields product
            var productBodyCat = new {
                Model = new {
                   Id = productId,      
                   // 🟨 Add your category fields here
                    categoryFields = new {
                     groups = new[] {
                        new {
                        SystemName = "External_Item_Description",  // ✅ Category ID (not field name)
                        Fields = new[] {
                            new { SystemName = "External_Item_Number", Value = External_Item_Number },
                            new { SystemName = "External_Item_Description", Value = External_Item_Description },
                            new { SystemName = "Vendor_Name", Value = Vendor_Name },
                            new { SystemName = "Project_Location", Value = Project_Location },
                            new { SystemName = "Selling_Currency", Value = Selling_Currency }
                        }
                    }
                    }
                    }                 }
            };
 
            var jsonCat = JsonSerializer.Serialize(productBodyCat);
            var contentCat = new StringContent(jsonCat, Encoding.UTF8, "application/json");
            var responseCat = client.PostAsync("/admin/api/ProductSave", contentCat).Result;
            var responseJsonCat = responseCat.Content.ReadAsStringAsync().Result;
 
            return $"✅ Product saved and added to group successfully (ID: {productId})! JSON Sent to API:" + jsonCat.ToString() + "Response JSON:" + responseJsonCat;
        }
        catch (Exception ex)
        {
            return $"❌ Exception: {ex.Message}";
        }

 


Replies

 

You must be logged in to post in the forum