Service api - Add entry with block as property (with required field)

Vote:
 

Using the service api (episerver.serviceapi.commerce v5.4.5) I am trying to add a product to a catalog, but I can't seem to get it to work.

Our product has a Header property that is a block with a required field, like this (simplified):

[CatalogContentType(DisplayName = "Default Product", MetaClassName = "Default_Product", GUID = "8892f392-ab91-41c5-8d3c-31ca80a1a02d")]
public class DefaultProduct : ProductContent
{
        [Display(Name = "Header", Description = "", GroupName = "Header", Order = 10)]
        public virtual HeaderBlock HeaderBlock { get; set; }
}

And the header block looks like this (simplified):

[ContentType(DisplayName = "Header Component", GUID = "625221a8-e7e4-4d78-b099-e8d472b83ed2")]
public class HeaderBlock : BlockData
{
      [Display(Name = "Title", GroupName = HeaderTabNames.Body, Order = 20)]
      [CultureSpecific]
      [Required]
      public virtual string Title { get; set; }

      [Display(Name = "Summary", GroupName = HeaderTabNames.Body, Order = 25)]
      [CultureSpecific]
      [UIHint(UIHint.Textarea)]
      public virtual string Summary { get; set; }
}

Every time I try to add a product like so, I get an exception with the message "Property 'Title' is required."

My code to add a product currently looks like this:

var prod = new Entry()
{
    Code = "T-PRD-01",
    Catalog = "My Catalog",
    EndDate = DateTime.UtcNow.AddDays(100),
    EntryType = "Product",  
    InventoryStatus = "Disabled", 
    IsActive = false,
    MetaClass = "Default_Product",
    Name = "Test import product",
    StartDate = DateTime.UtcNow,
    MetaFields = new List<MetaFieldProperty>
    {
        new MetaFieldProperty
        {
            Name = "EPiBlock_HeaderBlock_Title",
            Type = MetaDataType.LongString.ToString(),
            Data = new List<MetaFieldData>
            {
                new MetaFieldData
                {
                    Language="nl",
                    Value = "Test Product NL"
                }
            }
        },
        new MetaFieldProperty
        {
            Name = "DisplayName",
            Type = "ShortString",
            Data = new List<MetaFieldData>
            {
                new MetaFieldData
                {
                    Language="nl",
                    Value = "Test Product NL"
                }
            }
        }
    },
    SeoInformation = new List<SeoInfo>()
    {
        new SeoInfo()
        {
            Description = "description",
            Keywords="",
            LanguageCode = "nl",
            Title = "title",
            Uri = "whoknew",
            UriSegment = "whoknew"
        }
    }
};
private static void AddEntry(Entry product, JToken token)
 {
     var json = JsonConvert.SerializeObject(product);

     using (var client = new HttpClient())
     {
         client.BaseAddress = new Uri(_baseUri);
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.ToString());
         client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };

         var response = client.PostAsync("/episerverapi/commerce/entries", new StringContent(json, Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result;
     }
 }

How can I get this to work, and add a product with the service api, without having to change the required title property on the headerblock?

#253690
Apr 21, 2021 9:28
Vote:
 

Your Title and Summary are not properties, but subproperties, and they can't be represented by a MetaFieldProperty. You need to represent your (Entire) block as a MetaFieldProperty, and its data looks like this

<div data-classid="36f4349b-8093-492b-b616-05d8964e4c89" data-contentguid="44748655-f81e-4e3d-910a-5e11b273f8ac" data-contentname="New Block">{}</div>

You can manually update your block and look into database to see how it looks like 

 

#253691
Apr 21, 2021 9:52
Vote:
 

Manually changing the title for a product, and looking in the database. I see these values in the [CatalogContentProperty] table change as expected.

This is not what I would expect then. 

#253693
Apr 21, 2021 10:19
Quan Mai - Apr 21, 2021 10:35
hmm, it looks like I mixed the block with contentarea. if that is the case then you can use the name with EPiBlock_BlockName_Property then.
Vote:
 

I can only get this to work by setting the AllowNulls value for the Title property in the database to True. (allowing null value)

Passing in the property like EPiBlock_BlockName_Property does not seem to work. I can now create the product, but the title field remains empty.

#253695
Apr 21, 2021 11:13
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.