Sunday 12 September 2010

How to delete Custom Dotnetnuke 5.5 settings

After my previous post, we can insert and update host settings in DotNetNuke 5.5.

Now after some mistypes, I want to delete my bad custom settings.

Unfortunately as at inserting, we cannot use HostController because it provides only querying information, no update or insert, and however DataProvider has method for adding new setting but there is no method for delete.
Now we know what we can use so let's search something useful. After some thinking and searching in DotNetNuke api I realized that: there is nothing for delete host setting. At this point I decided, I return to the pure sql and there is a simple solution:

public static void InsertSetting(string key, string value)
{
  var dp = DataProvider.Instance();
                
  dp.AddHostSetting(key, value, false, -2);   // -2 is not an existing user. -1 is system default
 
  DotNetNuke.Common.Utilities.DataCache.ClearHostCache(true);
}



There is a new DotNetNuke api method there:
ClearHostCache(bool Cascade)
The explanation is simple:  HostController.Instance is a cache object and after deleting from database, we have to refresh the host cache.


And of course after the work, we can open a cold beer...

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 :)

Plans are changing...

You can plan everything you want but usually you cannot finish your planings.
So after weeks I can write again. I droped my original plans and going to start with something else now.