This project is read-only.

How to create or get an Instance of IContentManager (not from Controller, Driver, View)

Topics: Customizing Orchard
Oct 2, 2014 at 3:16 PM
Hello, could you please tell me how can i have access to ContentManager into a custom code (NOT in Controller, Driver, View). I want to use for a scheduled proccess and I need access to ContentManager to create some records of a custom ContentType.

Thank you
Oct 3, 2014 at 12:34 AM
Edited Oct 3, 2014 at 12:36 AM
As far as I know you have to schedule your task somewhere. Where is this? In an Controller or something where you have access to the ContentManager? If this is the case you can pass a instance of the ContentManager with a jobmap to your task. Something like this
 IJobDetail job = JobBuilder.Create<EmailJob>()
                .WithIdentity(JOP_NAME, JOP_GROUP)

 job.JobDataMap[Globals.CONTENT_MANAGER_KEY] = _instanceOfContentManager;
Oct 3, 2014 at 10:15 AM
Edited Oct 3, 2014 at 10:17 AM
Hello benschi11,

the initialization of is in Application_Start() method of Global.asax file. So if there is another way to get an instance of ContentManager inside Application_Start, then I can i could try your way.

Also insteed of that i could try to get an instance of ContentManager inside the Scheduled method of quartz. For example
    public class FeedCrawlerJob : IJob
        private static ILog _log = LogManager.GetLogger(typeof(FeedCrawlerJob));

        public FeedCrawlerJob()

        public void Execute(IJobExecutionContext context)
                JobDataMap dataMap = context.JobDetail.JobDataMap;
                string jobSays = dataMap.GetString("CustomStringParameter");
                SyndicationFeed feed;
                var feedUri = new Uri("");
                using (XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
                    feed = SyndicationFeed.Load(reader);

                var model = feed.Items.ToList().OrderByDescending(i => i.LastUpdatedTime)
                                .Select(i => new FeedItemPartRecord
                                    Title = i.Title.Text,
                                    Content = i.Summary.Text,
                                    DateTimeAdded = DateTime.Now,

               var _contentManager = /* Get an instance of ContentManager */

                foreach (var item in model)
But the problem is that i don't know how to create an instance of ContentManager. Can anyone help me? I'm new to Orchard.

Thank you
Oct 3, 2014 at 6:43 PM
Orchard has support for scheduled tasks. Don't use an external library, as that will make it a lot more difficult to inject dependencies such as ContentManager.
Marked as answer by padimen on 10/7/2014 at 3:00 AM
Oct 3, 2014 at 10:04 PM
Oh, thats true. Never used it before :) . Here is a link: OrchardDojo
Oct 6, 2014 at 9:00 AM
Edited Oct 6, 2014 at 9:00 AM
Thank you for your response,

but what if I need to create an instance of ContentManager into another part of code e.g. into a Custom class? Is that possible?

Thank you
Oct 6, 2014 at 9:24 PM
If your custom class is registered with the IoC container, you can simply inject an IContentManager via your class' constructor.
For example:
public interface IMyDomainService : IDependency {
   void DoSomething();

public class MyDomainService : IMyDomainService {
   private IContentManager _contentManager;

   public MyDomainService( IContentManager contentManager ) {
      _contentManager = contentManager;

   public void DoSomething() {
      // Use the content manager.
Because the sample class implements an interface that itself derives from IDependency, your class will be registered with Autofac.
Now you can inject your class into any other class, such as controllers, handlers and drivers.
Marked as answer by padimen on 10/7/2014 at 2:54 AM