Search code examples
c#.net-coreorchardcmsorchard-modulesorchardcore

Assign / Add Content Item To ListPart Orchard Core Programmatically


I have an API to create contentItem, but how can i assign it to other content's ListPart ? for example i have a Audio Album that has a ListPart with content type Audio Item.

currently i'm using content manager

 var contentItem = await _contentManager.NewAsync("AudioItem");

 await _contentManager.CreateAsync(contentItem, VersionOptions.DraftRequired);

 contentItem.DisplayText = name;

 await _contentManager.UpdateAsync(contentItem);

Thank you


Solution

  • You need to use the Weld and Alter extensions to modify a content item.

    contentItem.Weld<ContainedPart>();
    contentItem.Alter<ContainedPart>(x => x.ListContentItemId  = "id-of-parent");
    

    Weld will add the ContainedPart, and Alter will changes it's value.