This project is read-only.

Passing Parameters when Instantiating Parts programmatically.

Topics: Customizing Orchard
Aug 2, 2013 at 4:44 PM
I want to have additional parameters when Part is being instantiated. Currently, I overload the following method in the handler..
I create parts via ContentManager.New(contentType).
Where do I pass additional parameters ?
protected override void Initializing(InitializingContentContext context)
if (context.ContentType.Equals("SomeContentType"))
var part = context.ContentItem.As<SomePart>();
Aug 4, 2013 at 5:11 AM
What do you mean parameters? What would those do?
Aug 4, 2013 at 5:19 PM
BertrandLeRoy wrote:
What do you mean parameters? What would those do?
Hi Bertrand,
Here's some other additional background info that I should also provide, it may or may not be relevant:
1) I'm not using ContentPartRecord
2) Not leveraging Orchard's DataAccessLater. Instead I'm using external WebServices.

Getting back to your question, I call ContentManager.New(contentType) from my controller (eg: http://base/someaction/{id}).
I want to do, ContentManager.New(contentType,{id}).

Since Part does not know anything about controller, how do I achieve this?
Aug 5, 2013 at 1:29 AM
Still don't understand: there is no New method on ContentManager that takes two parameters. What is that id parameter supposed to do?
Aug 5, 2013 at 5:31 AM
I know that ContentManager.New takes in 1 param. When that gets called, the Initializing method gets invoke when an instance of a Part is created. The Id could be some transaction ID as an example..

so the sample I provided..

var part = context.ContentItem.As<SomePart>();
// part.TransactionID <-- Default to 0. Think of Constructor with param. How to get data from controller into the handler ? One way is via session, but before I go that route, I need to know if there's any other way?
Aug 5, 2013 at 5:38 AM
Perhaps keep it simple: load / create your content item, then initialize it further after you have an instance. E.g.
var contentItem = _contentManager.New<MyPart>("MyType");
contentItem.TransactionId = ...;
Aug 5, 2013 at 5:48 AM
I could have already done that. The key thing is, I want to do all my fetch/data manipulation from the my derived ContentHandler class.
So I could potentially handled it in the Initializing event. If not, in the OnLoading Event.

I tried overloading the OnLoading event, and this event never gets raised.
Aug 5, 2013 at 5:50 AM
Could you explain why you want to do that, instead of from your controller, or from your own service class which has a Get or Load method, accepting "constructor" arguments?
Aug 5, 2013 at 3:02 PM
Edited Aug 5, 2013 at 3:03 PM
Isn't it best-practices to do all the fetch/update from the handler class ?
In otherwords, I want to do some shapes manipulation before rendering it.

What I ended up doing is, in the constructor of my handler I did this, which I follow from the blogpost Handler:

Within this static method:-
private static void SetModelPropertiesEditor(BuildShapeContext context, SomePart part)
            if (context.ContentItem.ContentType.Equals("MyType"))
                      int _someId = part.TransactionId <-- This point in time, it already got initialized.
                      MyWcfProxyService.GetData(_someId) // I can now fetch data an do any data manipulation..
I believe this suits my scenario...