How can I create a list of content items via code in a module?

Feb 7, 2011 at 12:26 AM
Edited Feb 7, 2011 at 12:33 AM

I have a module that is similar to the address module.  But instead of placing states into a database, i would like to create the states into an Orchard list. How can this be done via code?

Feb 7, 2011 at 4:10 PM

Ok, I have found a way to add Content Items via here http://www.orchardproject.net/docs/content-types.ashx, but now I am trying to access the ContentManager from the migrations class.  The reason i am going this route is because, I am in the midst of building an address module that will include an address type, ie. Home, Work, etc. When the module is enabled I want to add the default content items to Address Type's list.  Then allow a non-developer to add more if needed later via the admin page. So I am building this in steps to learn Orchard Development.  

I have this thus far but stuck at how to load the ContentManager in Migrations.cs: 

public int UpdateFrom2()
        {
            Addresses.Controllers.AddressTypeController _contentManager = new Controllers.AddressTypeController(this.);
            return 3;
        }

Feb 7, 2011 at 5:10 PM

Ok it looks like this is done in the Handler via OnActivating.

Feb 8, 2011 at 2:27 AM

Handler isn't the area i was looking for.  

I looked at another module that implemented an Interface called 

IFeatureEventHandler

this has events such as:
 	public void Installing(Feature feature)
        {
            
        }

        public void Installed(Feature feature)
        {
            
        }

        public void Enabling(Feature feature)
        {
            
        }

        public void Enabled(Feature feature)
        {
            var featureName = feature.Descriptor.Id;
            _addressTypeService.AddressType_OnEnabled();  
        }

        public void Disabling(Feature feature)
        {
            
        }

        public void Disabled(Feature feature)
        {
            
        }

        public void Uninstalling(Feature feature)
        {
            
        }

        public void Uninstalled(Feature feature)
        {
            
        }

In my service I have this for when i enable the feature:
public void AddressType_OnEnabled(){
            
            var Work = _contentManager.Create("AddressType", item => {
                item.Record.Title = "Work";
            });
            var Home = _contentManager.Create("AddressType", item =>
            {
                item.Record.Title = "Home";
            });
        }

 Now onto creating the ContentType by code.