Thursday 9 September 2010

How to extend DotNetNuke 5.5 settings with customs

I wanted to put some custom settings into my DotNetNuke for storing values that I can use with all custom modules what I make (for example wcf service url for my data) but I didn't want to make a new table for my custom settings in my database and of course I didn't want to type lot.

The idea is taking some settings into DotNetNuke without:
- Creating a new table
- Writing sql scripts
- Using nuke api.
- Using simple key-value pairs
- Using fresh technologies (this time DotNetNuke at 5.5)

First of all, let's search where we can store key-value pairs in the DotNetNuke database. After some search I found two suitable tables: PortalSettings and HostSettings:As we can see these tables are similar: They both has SettingName and SettingValue columns. The others are not important to us. We can use HostSettings for host level settings and PortalSettings for everything that my portals need.
At my solution, I use host settings because I need them at multiple portals. I didn't check but I think you can do similar with portal settings too. CreatedByUserID values are existing users in our portal. Default settings that set during the installation has -1 CreatedByUserID value.


Now we know where we want to put our key-value pairs, the question is: how?

After using google a bit and trying many (really many) obsolete methods I found a solution that maches for my DotNetNuke version (5.5).

Steps are:

1.: Usings. Many blogs, forums, documentations telling you what to do but you don't know a bit information: Wich namespaces do I need?
using System;
using System.Linq;
using DotNetNuke.Entities.Controllers;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Data;

3.: We need a texbox for showing the results (markup):
<asp:Label runat="server" ID="lblSetting" BackColor="AntiqueWhite"/>

2.: Create custom host setting (C#):
var dp = DataProvider.Instance();
 
// -2 Not an existing user 
// Signature: AddHostSetting(
//      string SettingKey
//      , string SettingValue
//      , bool SettingIsSecure
//      , int createdByUserID);
dp.AddHostSetting("TestKey""TestValue"false, -2);

3.: Getting Setting value by key (C#):
lblSetting.Text = HostController.Instance.GetString("TestValue");
 
4. Open a beer and be happy :)

No comments:

Post a Comment