0

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

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

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

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

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

2 回答 2

0
   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

于 2021-12-19T13:14:55.203 回答
0

我能够解决它。事实证明,您Codename甚至可以在将新组添加到 Kontent 之前将其用作参考。然后对该模型中现有元素的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

于 2021-12-20T17:18:19.520 回答