Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Joseph May 11 posts 104 karma points
    Jun 23, 2020 @ 13:19
    Joseph May
    0

    Can't create content using Umbraco.Headless.Client.Net management service

    Hello,

    I am attempting to use the Umbraco.Headless.Client.Net .NET library to create some content. I get a 422 "Unprocessable Entity" response.

    Looking at the API documentation, 422 actually means a validation issue, however it gives no indication of what exactly is failing validation.

    All of the samples in the GitHub project seem related to the CD, not the CM service and there's no documentation I can find, so I am left to debug the client library. And it seems it was published to NuGet without symbols :(

    So, here's an example of my code. Does anyone know what's missing or what might fail validation?

            // using ManagementContent = Umbraco.Headless.Client.Net.Management.Models.Content;
    
            var folder = new ManagementContent
            {
                ParentId = Guid.Parse("40dcbf29-9dca-45a2-a33f-5cb80275ecd7"), // known ID of parent folder.
                ContentTypeAlias = "contentFolder" // content alias of a doctype with permission to be created under the parent. This doctype has no fields/validation requirements.
            };
    
            await _contentManagementService.Content.Create(folder);
    

    This results in:

    Refit.ApiException: Response status code does not indicate success: 422 (Unprocessable Entity).
       at Refit.RequestBuilderImplementation.<>c__DisplayClass14_0`2.<<BuildCancellableTaskFuncForMethod>b__0>d.MoveNext() in d:\a\1\s\Refit\RequestBuilderImplementation.cs:line 277
    

    Thanks,

    Joe

  • Rasmus John Pedersen 13 posts 462 karma points hq c-trib
    Jun 23, 2020 @ 13:34
    Rasmus John Pedersen
    100

    Hi Joe

    You should be able to get a more detailed error message by catching the ApiException and calling var error = await exception.GetContentAsAsync<ErrorResponse>();, see https://github.com/umbraco/Umbraco.Headless.Client.Net/blob/ec50cec27f67cc282fbcc4a25d353aad4d8f0dfa/test/Umbraco.Headless.Client.Net.Tests/Management/FormServiceFixture.cs#L121 for an example.

    From the looks of it the content is missing a Name which is required by Umbraco.

  • Joseph May 11 posts 104 karma points
    Jun 23, 2020 @ 13:47
    Joseph May
    0

    Thanks to your suggestion to catch an ApiException, I was able to get the specific error and learn that what it really wants is a name with the key "$invariant"

    So my updated code works now. Thanks for your help!

            // using ManagementContent = Umbraco.Headless.Client.Net.Management.Models.Content;
    
            var folder = new ManagementContent
            {
                ParentId = Guid.Parse("40dcbf29-9dca-45a2-a33f-5cb80275ecd7"), // known ID of parent folder.
                ContentTypeAlias = "contentFolder" // content alias of a doctype with permission to be created under the parent. This doctype has no fields/validation requirements.
            };
    
            folder.Name.Add("$invariant", "TestFolder");
    
            try
            {
                await _contentManagementService.Content.Create(folder);
            }
            catch (ApiException ex)
            {
                // Do some error handling.
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft