Skip to content

Instantly share code, notes, and snippets.

@jbubriski
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbubriski/0e9a6fa87c86c6c52177 to your computer and use it in GitHub Desktop.
Save jbubriski/0e9a6fa87c86c6c52177 to your computer and use it in GitHub Desktop.
T4 template for creating strongly typed properties for Kentico Custom Settings.
<#@ template debug="True" hostspecific="True" language="C#" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Configuration" #>
<#@ assembly name="System.Core" #>
<#@ Assembly Name="System.Collections" #>
<#@ Assembly Name="System.Data" #>
<#@ Assembly Name="System.Linq" #>
<#@ Assembly Name="System.Xml" #>
<#@ Assembly Name="System.Xml.Linq" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ Import Namespace="System.Configuration" #>
<#@ Import Namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ Import Namespace="System.Xml" #>
<#@ Import Namespace="System.Xml.Linq" #>
<#
var namespaceName = "MyProject";
#>
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
namespace <#= namespaceName #>
{
<#
var path = Host.ResolvePath(@"../../Web.config");
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
var connectionString = config.ConnectionStrings.ConnectionStrings["CMSConnectionString"].ConnectionString;
var sqlCommandText = @"SELECT DISTINCT(KeyName), KeyType
FROM CMS_SettingsKey
WHERE KeyIsCustom = 1";
#>
public partial class CustomSettings
{
<#
using (var sqlConnection = new SqlConnection(connectionString))
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection))
{
sqlCommand.Connection.Open();
using (var reader = sqlCommand.ExecuteReader())
{
while (reader.Read())
{
var keyName = reader["KeyName"].ToString();
var keyType = reader["KeyType"].ToString();
if (keyType == "string")
{
#>
public static string <#= keyName #>
{
get
{
var settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>", CMSContext.CurrentSiteID);
if (settingsKeyInfo != null)
{
if (settingsKeyInfo.KeyValue != null)
{
return settingsKeyInfo.KeyValue;
}
else
{
settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>");
if (settingsKeyInfo != null)
{
return settingsKeyInfo.KeyValue;
}
}
}
return "";
}
}
<#
}
else if (keyType == "boolean")
{
#>
public static bool <#= keyName #>
{
get
{
var settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>", CMSContext.CurrentSiteID);
if (settingsKeyInfo != null)
{
if (settingsKeyInfo.KeyValue != null)
{
return settingsKeyInfo.GetBooleanValue("KeyValue", false);
}
else
{
settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>");
if (settingsKeyInfo != null)
{
return settingsKeyInfo.GetBooleanValue("KeyValue", false);
}
}
}
return false;
}
}
<#
}
else if (keyType == "int")
{
#>
public static int <#= keyName #>
{
get
{
var settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>", CMSContext.CurrentSiteID);
if (settingsKeyInfo != null)
{
if (settingsKeyInfo.KeyValue != null)
{
return settingsKeyInfo.GetIntegerValue("KeyValue", 0);
}
else
{
settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>");
if (settingsKeyInfo != null)
{
return settingsKeyInfo.GetIntegerValue("KeyValue", 0);
}
}
}
return 0;
}
}
<#
}
else if (keyType == "double")
{
#>
public static double <#= keyName #>
{
get
{
var settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>", CMSContext.CurrentSiteID);
if (settingsKeyInfo != null)
{
if (settingsKeyInfo.KeyValue != null)
{
return settingsKeyInfo.GetDoubleValue("KeyValue", 0);
}
else
{
settingsKeyInfo = SettingsKeyProvider.GetSettingsKeyInfo("<#= keyName #>");
if (settingsKeyInfo != null)
{
return settingsKeyInfo.GetDoubleValue("KeyValue", 0);
}
}
}
return 0;
}
}
<#
}
}
}
}
#>
} // End class
} // End namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment