-
Hi Everyone, We previously discussed a similar topic here. Here's how the Content Type structure is defined:
To clarify the structure, here's the JSON representation of the {
"ContentItemId": "41p7rs7vdswcy6r84je6xjjb45",
"ContentItemVersionId": "40qgvgs9veeybzncagmc7pdznz",
"ContentType": "PublisherManager",
"DisplayText": null,
"Latest": true,
"Published": true,
"ModifiedUtc": "2024-05-08T17:38:36.353494Z",
"PublishedUtc": "2024-05-08T17:38:48.3510301Z",
"CreatedUtc": "2024-05-08T13:48:19.9678749Z",
"Owner": "46ppk5a27b3081v2sgqe3bm8ms",
"Author": "MarcoDev",
"PublisherManager": {
"PublisherManagerId": {
"Text": "TestManager1234"
},
"UserId": {
"Text": "TestUser1"
}
},
"BagPart": {
"ContentItems": [
{
"ContentItemId": "4qrg3a8jscerd1wvtfz8n0cgd5",
"ContentItemVersionId": null,
"ContentType": "PublisherPublisherManagerRelation",
"DisplayText": "",
"Latest": false,
"Published": false,
"ModifiedUtc": "2024-05-08T17:38:36.3742357Z",
"PublishedUtc": null,
"CreatedUtc": null,
"Owner": "46ppk5a27b3081v2sgqe3bm8ms",
"Author": "MarcoDev",
"PublisherPublisherManagerRelation": {
"Publisher": {
"ContentItemIds": [
"4w7zb827439vk6gq7586jjjkmw" // Content Picker Field
]
},
"PublisherId": {
"Text": "TestPublisher1234"
},
"SalesManagerFullname": {
"Text": "Pippo Pluto"
},
"Email": {
"Text": "[email protected]"
},
"Phone": {
"Text": null
},
"WhatsApp": {
"Text": null
},
"Website": {
"Text": null
},
"Instagram": {
"Text": null
},
"Facebook": {
"Text": null
},
"Description": {
"Text": "Ciao, this is a descritption"
}
},
"BagPart": {
"ContentItems": [
{
"ContentItemId": "4w6x47q4vmt6432t330bxqj9dh",
"ContentItemVersionId": null,
"ContentType": "PublishingDistributor",
"DisplayText": null,
"Latest": false,
"Published": false,
"ModifiedUtc": "2024-05-08T17:38:36.4186638Z",
"PublishedUtc": null,
"CreatedUtc": null,
"Owner": "46ppk5a27b3081v2sgqe3bm8ms",
"Author": "MarcoDev",
"PublishingDistributor": {
"Distributors": { // Multi Text Fields
"Values": [
"1",
"2"
]
}
}
}
]
}
}
]
}
} When attempting to update, my In this case with nested
If you have any suggestions, please feel free to reply. Here's the code that is not working: public async Task<ApiResult<PublisherManagerApiModel>> UpsertPublisherManagerAsync(PublisherManagerApiModel model)
{
// Validate the model
// [...] omitted
bool isUpdate = !string.IsNullOrEmpty(model.ContentItemId);
ContentItem contentItem = isUpdate ? await _contentManager.GetAsync(model.ContentItemId, VersionOptions.Published)
: await _contentManager.NewAsync(nameof(PublisherManager));
if (contentItem == null)
{
return new ApiResult<PublisherManagerApiModel>(HttpStatusCode.NotFound, "PublisherManager not found.");
}
// Manage the PublisherManager part
PublisherManager publisherManagerPart = contentItem.As<PublisherManager>() ?? new PublisherManager();
publisherManagerPart.PublisherManagerId.Text = model.PublisherManagerId;
publisherManagerPart.UserId.Text = model.UserId;
// Manage the BagPart
BagPart bagPart = contentItem.GetOrCreate<BagPart>();
if (isUpdate)
{
bagPart.ContentItems.Clear(); // Clear existing items if updating
}
// Handle relations as child content items
foreach (PublisherPublisherRelation relation in model.Publishers)
{
ContentItem childContentItem = await _contentManager.NewAsync("PublisherPublisherManagerRelation");
childContentItem.Alter<PublisherPublisherManagerRelation>(nameof(PublisherPublisherManagerRelation), childPart =>
{
childPart.SalesManagerFullname.Text = relation.SalesManagerFullname;
childPart.Email.Text = relation.Email;
childPart.Phone.Text = relation.Phone;
childPart.Whatsapp.Text = relation.Whatsapp;
childPart.Website.Text = relation.Website;
childPart.Instagram.Text = relation.Instagram;
childPart.Facebook.Text = relation.Facebook;
childPart.Description.Text = relation.Description;
// Populate Publishers
childPart.Publishers.Clear();
foreach (string publisherId in relation.RelatedPublishers.ContentItemIds)
{
childPart.Publishers.Add(new TextField { Text = publisherId });
}
// Populate DistributorValues
childPart.DistributorValues.Clear();
foreach (int distributor in relation.DistributorValues)
{
PublishingDistributor newDistributorPart = new PublishingDistributor
{
Distributors = new MultiTextField
{
Values = new[] { distributor.ToString() }
}
};
childPart.DistributorValues.Add(newDistributorPart);
}
});
bagPart.ContentItems.Add(childContentItem);
}
// Persist changes
if (isUpdate)
{
await _contentManager.UpdateAsync(contentItem);
}
else
{
await _contentManager.CreateAsync(contentItem, VersionOptions.Published);
}
await _contentManager.PublishAsync(contentItem);
return new ApiResult<PublisherManagerApiModel>(HttpStatusCode.OK, model);
} This is my public class PublisherManagerApiModel
{
public string ContentItemId { get; set; } // Additional property compared to the BO
public string UserId { get; set; }
public string PublisherManagerId { get; set; }
public List<PublisherPublisherRelation> Publishers { get; set; }
}
public class PublisherPublisherRelation
{
public RelatedPublishers RelatedPublishers { get; set; }
public string SalesManagerFullname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string? Whatsapp { get; set; }
public string? Website { get; set; }
public string? Instagram { get; set; }
public string? Facebook { get; set; }
public string? Description { get; set; }
public List<int> DistributorValues { get; set; }
}
public class RelatedPublishers
{
public List<string> ContentItemIds { get; set; }
} This is how I have represented the using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
public class PublisherManager : ContentPart
{
public TextField PublisherManagerId { get; set; } = new();
public TextField UserId { get; set; } = new();
public List<PublisherPublisherManagerRelation> PublisherRelations { get; set; } = new();
}
public class PublisherPublisherManagerRelation : ContentPart
{
// Publisher picker
public List<TextField> Publishers { get; set; } = new();
// Other properties
public TextField PublisherId { get; set; } = new();
public TextField SalesManagerFullname { get; set; } = new();
public TextField Email { get; set; } = new();
public TextField Phone { get; set; } = new();
public TextField Whatsapp { get; set; } = new();
public TextField Website { get; set; } = new();
public TextField Instagram { get; set; } = new();
public TextField Facebook { get; set; } = new();
public TextField Description { get; set; } = new();
// Distributors select
public List<PublishingDistributor> DistributorValues { get; set; } = new();
}
public class PublishingDistributor : ContentPart
{
public MultiTextField Distributors { get; set; } = new();
} Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
I think there could be a binding problem because how I represented the |
Beta Was this translation helpful? Give feedback.
@Piedone in the end, I was able to make it work 🥳 but I think we need more deep and accurate documentation on this, because it's something about the behavior Orchard Core framework expects.
The problem was how I was doing the
Alter
on theBagPart
, and how I was doing the add of theBagPart
to my content item. In fact, there's a strict way to do it, as fortunately @deanmarcussen explained some years ago here, and it is like:Alter
theBagPart
content item (always get/create theBagPart
through theContentManager
in DI);BagPart
to your maincontentItem
;CreateAsync
orUpdateAsync
, etc.);