This project is read-only.

Create blog post from within custom module

Topics: Writing modules
Jun 28, 2012 at 10:47 AM

I'm working on module that should receive data from outer source and make blog posts.

My previous question was about how to reference and use other module:

After referencing Orchard.Blogs and exploring code I found that there is suddenly no code that actually create posts:) My hope was to find something like BlogManager.MakePost() and call it... well, I'm just not get used yet to this magic CMS ways.

How could I create blog post?

Thank you.

Jun 28, 2012 at 2:10 PM
Edited Jun 28, 2012 at 2:11 PM

What do you mean: first there was code, but now there isn't? Maybe hit the wrong button by accident? It happens ;)
Anyway, there actually is code that creates a post. It's in the Create method of the BlogPostAdminController:

var blogPost = Services.ContentManager.New<BlogPostPart>("BlogPost");

The key thing I think to understand here is that a blog post really is just another sort of content item, so when you need to create new content, use the ContentManager to do it.

Jun 29, 2012 at 4:55 AM

I meant that there is no code that can be accessed easily from the outside. I cannot create instance of BlogPostAdminController in my code and invoke CreatePOST. So it helps just for example and copy-paste.

Jun 29, 2012 at 10:31 AM
Edited Jun 29, 2012 at 10:33 AM

Well, now in my module I'm using following code to create blog posts:

var blog = _blogService.Get(blogIdFromMySettings, VersionOptions.Latest).As<BlogPart>();
var blogPost = _services.ContentManager.New<BlogPostPart>("BlogPost");
blogPost.BlogPart = blog;
blogPost.Title = "test title";
blogPost.Text = "test text";
_services.ContentManager.Create(blogPost, VersionOptions.Draft);

It works well while I call this code as the response for button click in my view. But when I'm trying execute it using IBackgroundTask interface I'm catching exception at the last line. This exception makes me sad because it shows me code from the core that wants access to current HttpContext and as there is no context it will always fail. I don't ask why they need HttpContext to save Blog Post to database:( I'm sure that I'm just using something wrong.

Thank you!

Jun 29, 2012 at 12:03 PM

Perhaps it's not the content manager that needs the HttpContext (that would make me sad as well), but perhaps there is a content handler somewhere that does have a dependency on HttpContext. When you create content, the ContentManager invokes a Creating and Created event (which are methods on IContentHandler).

I remember someone having a similar issue who required an HttpContext in a background task, and someone had a solution for this. Although I can't remember what that solution was exactly, if you do a search on that you may find that thread.

Jun 29, 2012 at 12:14 PM

Do you mean that one of the other modules may listen to Create or Created events of BlogPostPart and cause this exception?

Jun 29, 2012 at 2:01 PM

Possibly, yes. You should be able to check for this by attaching the debugger to your web server process. Also make sure you have the "Exceptions" settings "Common Language Runtime Exceptions" and "Managed Debugging Assistants" set to "Thrown" so that the debugger will break at the exact location where the exception occurs.

Jul 2, 2012 at 11:31 AM

I found out that it was bug and it's already fixed: