This project is read-only.

Inserting records on module load

Topics: Troubleshooting
Mar 5, 2011 at 3:50 PM
Edited Mar 5, 2011 at 3:51 PM

Hey guys I have a ContentHandler, and i want to insert multiple default values in to a table on startup.... how can I do this? Im doing this, but it keeps blowing up...

namespace NGM.OpenAuthentication.Handlers {
    public class OAuthProviderSettingsPartHandler : ContentHandler {
        private readonly IOrchardServices _orchardServices;

        public OAuthProviderSettingsPartHandler(IRepository<OAuthProviderSettingsPartRecord> repository, IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
            Filters.Add(new ActivatingFilter<OAuthProviderSettingsPart>("Site"));


        private void SetupDefaultSettings() {
            _orchardServices.ContentManager.Create<OAuthProviderSettingsPart>("Site", (o) => {
                o.Record.Provider = OAuthProvider.Facebook.ToString();
                o.Record.ClientIdentifier = "xx";
                o.Record.ClientSecret = "xx";

            _orchardServices.ContentManager.Create<OAuthProviderSettingsPart>("Site", (o) => {
                o.Record.Provider = OAuthProvider.Twitter.ToString();
                o.Record.ClientIdentifier = "yy";
                o.Record.ClientSecret = "yy";

            _orchardServices.ContentManager.Create<OAuthProviderSettingsPart>("Site", (o) => {
                o.Record.Provider = OAuthProvider.LiveId.ToString();
                o.Record.ClientIdentifier = "zz";
                o.Record.ClientSecret = "zz";

Mar 5, 2011 at 6:24 PM

You can't create a Site content item, there is already one, and there should be only one. I assume you are trying to define default values for the different Settings Parts here, and there are a few examples in Orchard already. You can use the DataMigration for instance, by loading the current Settings, and assigning values, very simple. But as they are parts to a single instance, there can be only one record. Your model might need to be changed.

var siteSettings = siteService.GetSiteSettings().As<OAuthProviderSettingsPart>();


Mar 7, 2011 at 8:56 PM

Have you tried handling the module lifetime events (implementing IFeatureEventHandler) and doing the startup stuff there?