WebApi post create - how to map paramaters to a ContentType

Topics: Customizing Orchard, Writing modules
Oct 31, 2012 at 6:32 PM

Looking for some guidance on how best to pass parameters to a web api in order to create an orchard content type.

So I have an orchard content type called Apartments and it is composed of several parts.

Take for example the title part:  In the html form (which is also used to create an apartment) it has the name "Title.Title".

So presumably if I pass an dynamic object with the key "Title.Title" I could pass it directly to the ContentManager and it would know which part it mapped to.  I could do the same for other parts too.

Does this seem reasonable? I'm not convinced

 public HttpResponseMessage PostApartment(dynamic item)
            return Request.CreateResponse();

Oct 31, 2012 at 6:35 PM

Why would you want to put a dynamic object as the parameter here? Looks like it will only confuse model binding.

Oct 31, 2012 at 7:39 PM
Edited Oct 31, 2012 at 7:45 PM

Fair point. That's not going to work is it.

So perhaps I have to create a modelview? Something like:


 public class ApartmentViewModel
        public int ApartmentId { get; set; }        
        public string Title { get; set; }        


But how would I map 'Title' here to my ContentType > TitlePart > Title field?

I'm a bit lost.

Edit: Actually that's not going to work either - it won't know what kind of ContentType it's creating will it. Stumped.

Oct 31, 2012 at 8:37 PM

You can tell the content manager what type to create using its New method. Once you've set the properties you need to set on the relevant parts is when you call create with the new content item.

Oct 31, 2012 at 10:30 PM
Edited Oct 31, 2012 at 10:31 PM

Here is an example...

public HttpResponseMessage PostApartment(ApartmentViewModel model)
    var contentItem = Services.ContentManager.New<ApartmentPart>("Apartment");
    contentItem.As<TitlePart>() = model.Title;
    contentItem.As<ApartmentPart>().ExternalId = model.ApartmentId;
    return Request.CreateResponse();

In this example I have made the assumption that ApratmentId is not an OrchardId

Nov 2, 2012 at 10:50 AM

Thanks for the excellent example.  Very useful.

I am struggling a bit to understand the relationship between a ContentItem and a ContentPart. 

My understanding: the Apartment ContentItem is composed of many ContentParts i.e. ApartmentPart, TitlePart

But when creating the new item in the code above it's: Services.ContentManager.New<ApartmentPart>("Apartment");

Which implies Apartment IS an ApartmentPart.


Nov 2, 2012 at 7:08 PM

A content part is a discrete piece of functionality, where as a contentitem is a grouping of those functionalities. Its what makes orchard so powerful.

I could have done Services.ContentManager.New("Apartment")

This would return a contentitem which I could then do As<ApartmentPart>()