【问题标题】:What is the process for adding Groups to an existing type in Kontent Management API v2 with C#?使用 C# 在 Kontent Management API v2 中将组添加到现有类型的过程是什么?
【发布时间】:2021-12-17 20:27:31
【问题描述】:

如果我只是尝试使用 Kontent Management API (v2) 将组添加到 现有 Kontent 类型,我会收到以下错误(请参阅下面的代码,它会生成此错误):

验证错误:511 每个元素在使用组时都应该有一个正确的内容组引用

在这种情况下,使用 C# 通过 Management API 添加组的过程是什么?我假设我需要首先将一个组 Reference 添加到所有现有元素,但是当我无法通过 API 添加组时,我该怎么做呢?我可以在将 new ContentGroupModel() 对象添加到 Kontent 中的实际类型之前简单地创建一个有效的 Reference 吗?

这是我现有的代码,它会引发上述错误:

var updatedType = await service.ModifyContentTypeAsync(
    Reference.ById(existingContentType.Id),
    new ContentTypeAddIntoPatchModel
    {
        Path = "/content_groups",
        Value = new ContentGroupModel
        {
            Name = "New Group",
            CodeName = "new_group"
        }
    }
);

【问题讨论】:

    标签: c# kentico-kontent


    【解决方案1】:
       using Kentico.Kontent.Management;
    
    var client = new ManagementClient(new ManagementOptions
    {
        ApiKey = "<YOUR_API_KEY>",
        ProjectId = "<YOUR_PROJECT_ID>"
    });
    
    var identifier = Reference.ById(Guid.Parse("0be13600-e57c-577d-8108-c8d860330985"));
    // var identifier = Reference.ByCodename("my_article");
    // var identifier = Reference.ByExternalId("my-article-id");
    
    var response = await client.ModifyContentTypeAsync(identifier, new ContentTypeOperationBaseModel[]
    {
        new ContentTypeReplacePatchModel
        {
            Path = "/name",
            Value = "A new type name"
        },
        new ContentTypeReplacePatchModel
        {
            Path = "/elements/codename:my_text_element/guidelines",
            Value = "Here you can tell users how to fill in the element."
        },
        new ContentTypeAddIntoPatchModel
        {
            Path = "/elements",
            Value = new TextElementMetadataModel
            {
                Name = "My title",
                Guidelines = "Title of the article in plain text.",
                ExternalId = "my-title-id",
            },
        },
        new ContentTypeRemovePatchModel
        {
            Path = "/elements/id:0b2015d0-16ae-414a-85f9-7e1a4b3a3eae"
        }
    });
    

    参考 - https://docs.kontent.ai/reference/management-api-v2#operation/modify-a-content-type

    【讨论】:

    • 感谢您的回答。不幸的是,这有点太笼统了。我在文档中看到了这一点,但它并没有完全解决如何将组添加到 现有 模型。
    【解决方案2】:

    我能够解决它。事实证明,即使在将其添加到 Kontent 之前,您也可以使用新组的 Codename 作为参考。然后将该引用提供给模型中现有元素的 content_group 属性/路径。

    这是我现在添加新组并将其动态添加到所有现有元素的代码:

    using Kentico.Kontent.Management.Models.Shared;
    using Kentico.Kontent.Management.Models.Types;
    using Kentico.Kontent.Management.Models.Types.Patch;
    
    //
    // ...
    //
    
    // First, Get type from the client. Then ...
    var elementCodenames = myType.Elements.Select(el => el.Codename);
    
    // Create reference to the codename of the group to be created
    var groupRef = Reference.ByCodename("new_group");
    
    // Create the modify models
    var modifyModels = new ContentTypeOperationBaseModel[] {
        new ContentTypeAddIntoPatchModel
        {
            Path = "/content_groups", // Add to content_groups path
            Value = new ContentGroupModel
            {
                Name = "New Group",
                CodeName = "new_group" // Same codename as above
            }
        }
    }
    .Concat(
        // Dynamically add new group, by ref, to existing elements
        elementCodenames.Select(codename => new ContentTypeReplacePatchModel
        {
            Path = $"/elements/codename:{codename}/content_group",
            Value = groupRef // Group reference created above from the codename
        })
    );
    
    // Make call to add new group AND link group to elements in the one call
    var response = await client.ModifyContentTypeAsync(myTypeIdentifier, modifyModels.ToArray());
    

    省略其他细节,例如检索现有类型,但此处参考 API 文档:https://docs.kontent.ai/reference/management-api-v2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 2014-02-24
      • 2010-12-11
      • 2011-04-14
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多