This project is read-only.

Repeatable Parts

Topics: Administration, Customizing Orchard
Mar 16, 2015 at 8:35 PM
Wondering if it is possible to make a part repeatable or duplicate per content type, so it can be used once or 100 times.

For example, say I wanted to add multiple addresses to a user. I would create a part with street, city, zip, etc. Then when adding a user, I could add one address, or multiple by hitting some plus sign or something to attach another address part.
Mar 16, 2015 at 8:56 PM
You can implement that, but not in the way you suggest.
What you can do instead is implement something like an AddressesPart, which itself stores zero or more addresses. Each address could either be a record in the DB or an object in the content item's infoset storage.
Mar 16, 2015 at 9:41 PM
I think that is more or less what I was trying to say. I just have no idea how to add multiple.
Mar 17, 2015 at 12:03 AM
You can't add multiple parts, but if you go with the infoset storage route, it is a simple matter of storing a serialized XML or JSON string for example, representing your list of address objects. E.g.
// A POCO representing an individual address.
public class Address {
   public string Line1 { get; set; }
   public string Line2 { get; set; }
   public string PostalCode { get; set; }
   public string City { get; set; }
   public string Country { get; set; }

// A content part that stores multiple Address pocos.
public class AddressesPart : ContentPart {
   public IEnumerable<Address> Addresses {
      get {
         var json = Retrieve<string>("Addresses");
         return !String.IsNullOrWhiteSpace ? JsonConvert.DeserializeObject<List<Address>>(json) : new List<Address>(0);
      set {
         var json = JsonConvert.SerializeObject(value.ToList());
         Store("Addresses", json);

   public void AddAddress(Address address) {
      // Get the existing addresses as a list so that we can add one.
      var addresses = Addresses.ToList();

      // Add the address to the local list.

      // Store the local list into our property, causing the list to be serialized to JSON and stored in the infoset.
      Addresses = addresses;
Mar 17, 2015 at 7:08 PM
Awesome, I'll give this a shot. Thanks