使用 C# 在 Kontent Management API v2 中将组添加到现有类型的过程是什么?
What is the process for adding Groups to an existing type in Kontent Management API v2 with C#?
如果我只是尝试使用 Kontent Management API (v2) 添加一个组到 existing Kontent 类型,我会收到以下错误(请参阅下面的代码生成此错误):
Validation errors: 511 Every element should have a correct content group reference when using groups
在这种情况下,使用 C# 通过管理 API 添加组的过程是怎样的?我会假设我需要先将一个组 Reference
添加到所有现有元素,但是当我通过 API 添加组时我该怎么做?在将它添加到 Kontent 中的实际类型之前,我可以简单地使用 new ContentGroupModel()
对象创建一个有效的 Reference
吗?
这是我现有的代码,它抛出上述错误:
var updatedType = await service.ModifyContentTypeAsync(
Reference.ById(existingContentType.Id),
new ContentTypeAddIntoPatchModel
{
Path = "/content_groups",
Value = new ContentGroupModel
{
Name = "New Group",
CodeName = "new_group"
}
}
);
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
我能够解决这个问题。事实证明,您可以使用新组的 Codename
作为参考,甚至在将其添加到 Kontent 之前。然后为模型中的现有元素提供对 content_group property/path 的引用。
这是我现在拥有的添加新组并将其动态添加到所有现有元素的代码:
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
如果我只是尝试使用 Kontent Management API (v2) 添加一个组到 existing Kontent 类型,我会收到以下错误(请参阅下面的代码生成此错误):
Validation errors: 511 Every element should have a correct content group reference when using groups
在这种情况下,使用 C# 通过管理 API 添加组的过程是怎样的?我会假设我需要先将一个组 Reference
添加到所有现有元素,但是当我通过 API 添加组时我该怎么做?在将它添加到 Kontent 中的实际类型之前,我可以简单地使用 new ContentGroupModel()
对象创建一个有效的 Reference
吗?
这是我现有的代码,它抛出上述错误:
var updatedType = await service.ModifyContentTypeAsync(
Reference.ById(existingContentType.Id),
new ContentTypeAddIntoPatchModel
{
Path = "/content_groups",
Value = new ContentGroupModel
{
Name = "New Group",
CodeName = "new_group"
}
}
);
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
我能够解决这个问题。事实证明,您可以使用新组的 Codename
作为参考,甚至在将其添加到 Kontent 之前。然后为模型中的现有元素提供对 content_group property/path 的引用。
这是我现在拥有的添加新组并将其动态添加到所有现有元素的代码:
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