Automatically: Publish module, Add module on page creation

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
6/18/2008 2:51:36 AM
Gravatar
Total Posts 11

Automatically: Publish module, Add module on page creation

Hello,
For the site I am developing I had the need to automatically:
 - Add module on page creation
 - Publish certain module on page creation

The modules to be published/added is the same on every page. Just thought I'd share the solution in case someone else needs to do it later on (since you guys have helped me so much).

In admin/PageSettions.aspx.cs. Function SavePageData, after:

bool saved = pageSettings.Save();
pageId = pageSettings.PageId;

I added the following code:

int timeOffset = 2;
DateTime beginDate = DateTime.Parse("2008-06-15 04:00:01").AddHours(-timeOffset);
string end = "";
DateTime endDate = end.Length > 0 ? DateTime.Parse(end).AddHours(-timeOffset) : DateTime.MinValue;

// Publish module
Module.Publish(
pageSettings.PageGuid,
new Guid( "773a46d2-ba3d-4e24-9b72-c5735528ce74"),
17,
pageId,
"leftpane",
0,
beginDate,
endDate);

 


// Add html module
int moduleDefID = 1;
ModuleDefinition moduleDefinition = new ModuleDefinition(1);

Module m = new Module();
m.SiteId = siteSettings.SiteId;
m.SiteGuid = siteSettings.SiteGuid;
m.ModuleDefId = moduleDefID;
m.FeatureGuid = moduleDefinition.FeatureGuid;
m.Icon = moduleDefinition.Icon;
m.CacheTime = moduleDefinition.DefaultCacheTime;
m.PageId = pageId;
m.ModuleTitle = "Sidansvarig: ";
m.PaneName = "altcontent1";
m.AuthorizedEditRoles = "Admins";
m.CreatedByUserId = SiteUtils.GetCurrentSiteUser().UserId;
m.Save();

int moduleDefID2 = 1;
ModuleDefinition moduleDefinition2 = new ModuleDefinition(1);

Module m2 = new Module();
m2.SiteId = siteSettings.SiteId;
m2.SiteGuid = siteSettings.SiteGuid;
m2.ModuleDefId = moduleDefID2;
m2.FeatureGuid = moduleDefinition2.FeatureGuid;
m2.Icon = moduleDefinition2.Icon;
m2.CacheTime = moduleDefinition2.DefaultCacheTime;
m2.PageId = pageId;
m2.ModuleTitle = "Sidinnehåll";
m2.PaneName = "contentpane";
m2.AuthorizedEditRoles = "Admins";
m2.CreatedByUserId = SiteUtils.GetCurrentSiteUser().UserId;
m2.Save();
 

It's a quite ugly hack, and static too (since module IDs and all that is hard coded (which I don't recommend really)). But it works for my problem on this specific site. If someone need to do something similar to that in the future they can always get inspiration from this code (while I don't recommend you using it straight away).

Regards,

Niklas

6/18/2008 6:35:43 AM
Gravatar
Total Posts 18439

Re: Automatically: Publish module, Add module on page creation

Hi Niklas,

I'm glad you found a way to do what you wanted to do. However, in general, I recommend to avoid making changes in the core of mojoportal like in your example. Once you start down that path you make it almost impossible for yourself to get future upgrades and bug fixes of mojoportal because if you upgrade you will lose this customization or else have to add it again to the new version.

Sometimes it is impossible to meet requirements without making changes in the mojoportal code and maybe this case is one of them but maybe not.

There is one possible solution I can think of to put the same existing content on every page. We have a control named ModuleWrapper in Web/Controls folder. You can add it to your layout.mast of your skin and configure it to show a specific content instance on every page something like this:

<portal:ModuleWrapper id="mw1" runat="server" ConfigureModuleId="17" />

If I were trying to do the things you are doing in this code like adding a new Html module, I would create a control that wraps this logic and also put it in layout.master. The control could get a reference to the current page, check if the content has already been added and if not then add it.

The main thing I'm trying to emphasize is I recommend finding a way to keep all of your custom code in your own external projects rather than forking the mojoportal core by puting your customizations right in the core code. I'm working every day to improve mojoportal and I want everyone to be able to get the improvements going forward.

Best,

Joe

6/18/2008 6:44:53 AM
Gravatar
Total Posts 11

Re: Automatically: Publish module, Add module on page creation

You are absolutely right, core changes are usually bad. Probably here as well. The main problem in this project is that it's more focused on time then the perfect solution. I'll try to remake it, but I am not sure if we will have the time.

6/18/2008 7:02:11 AM
Gravatar
Total Posts 18439

Re: Automatically: Publish module, Add module on page creation

I know what you mean, I've been on projects like that many times myself too and sometimes your only choice is not that pretty and you just do what you have to and move on. The only thing I would worry about is if your customer knows the project is built on mojoportal and later long after the project is over  they decide to upgrade on their own that functionality will go away after the upgrade. I suppose the good news is it would not break the site if that code disappears, the site will still work without that code, but the custom functionality will be gone.

Best,

Joe

6/18/2008 7:07:31 AM
Gravatar
Total Posts 11

Re: Automatically: Publish module, Add module on page creation

True. I will give it some thought. Future updates will most likely be handled by us, but it would still be nice to be able to upgrade without breaking things. That way if we need to do a quick update in the future we can do so.

6/26/2008 5:06:36 PM
Gravatar
Total Posts 22
Kenneth Haugland

Re: Automatically: Publish module, Add module on page creation

 

One idea to make it easier to make such changes to mojoportal would be to make some kind of hooks one could use.

Easiest would be to make some kind of events, if this was a desktop application thats how I would do it but I do not know if this is a feasable solution when it comes to web pages.

Kenneth

6/27/2008 5:51:16 AM
Gravatar
Total Posts 18439

Re: Automatically: Publish module, Add module on page creation

Hi Kenneth,

Actually you are right, the best solution for what Niklas is trying to accomplish would be for me to implement a hook to make it easier to plug in the code he needs. I will try to implement this today and get it into svn trunk over the weekend, I think it will take only about an hour to code it up.

Basically I will setup a provider model for PageCreatedEventHandlers so that additional providers can be implemented in custom code and plugged in via a config file. Then I will create an event for PageCreated and in the PageSettings.aspx page I will hookup all the handlers before calling .Save() on the new page object. This way all custom handlers will get an opportunity to do what they need to do upon page creation.

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.