There is a method already but I don't believe there is a macro you can use.
SessionHelper.GetValue("sessionVariableName", defaultValue)
So although you don't want to write any custom code, it is very easy to do so. Also make sure your file structure follows the standards from Kentico. If you name the folder in the App_Code directory the same as the website name, it will export the code with it when you export the site. This method supports the upgrade path as well so no worries there. For instance your site code name is MyWebSite. Your file structure would be:
/App_Code/MyWebSite
/App_Code/MyWebSite/Classes
/App_Code/MyWebSite/Classes/MacroMethod.cs
/App_Code/MyWebSite/Modules
/App_Code/MyWebSite/Modules/CustomModuleLoaderMethod.cs
Below is the code I used to create a few macros to handle getting and setting session values.
CustomModuleLoaderMethod.cs class
using CMS.SettingsProvider;
[CustomClassLoaderModule]
public partial class CMSModuleLoader
{
/// <summary>
/// Summary description for CustomClassLoaderModuleAttribute
/// </summary>
public class CustomClassLoaderModuleAttribute : CMSLoaderAttribute
{
/// <summary>
/// Initializes the module
/// </summary>
public override void Init()
{
MyCustom.CustomMacroMethods.RegisterMethods();
}
}
}
MacroMethod.cs class
using CMS.GlobalHelper;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace MyCustom
{
public static class CustomMacroMethods
{
public static void RegisterMethods()
{
MacroMethod getSessionValue = new MacroMethod("GetSessionValue", GetSessionValue)
{
Comment = "Gets a session value by the key name.",
Type = typeof(object),
AllowedTypes = new List<Type>() { typeof(string) },
MinimumParameters = 1,
Snippet = "GetSessionValue(\"SessionKeyName\")"
};
getSessionValue.AddParameter("SessionKey", typeof(string), "Session key to retrieve a value from.");
MacroMethods.RegisterMethod(getSessionValue);
MacroMethod setSessionValue = new MacroMethod("SetSessionValue", SetSessionValue)
{
Comment = "Sets a session value by the key name.",
Type = typeof(bool),
AllowedTypes = new List<Type>() { typeof(string) },
MinimumParameters = 2,
Snippet = "SetSessionValue(\"SessionKeyName,SessionKeyValue\")"
};
setSessionValue.AddParameter("SessionKey", typeof(string), "Session key to set a value for.");
setSessionValue.AddParameter("SessionValue", typeof(object), "Session key's value to set.");
MacroMethods.RegisterMethod(setSessionValue);
}
/// <summary>
/// Wrapper method of GetSessionValue suitable for MacroResolver.
/// </summary>
/// <param name="parameters">array of parameters</param>
/// <returns></returns>
public static object GetSessionValue(params object[] parameters)
{
switch (parameters.Length)
{
case 1:
return GetSessionValue(ValidationHelper.GetString(parameters[0], ""));
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Gets the top level navigation from a URL
/// </summary>
/// <param name="URL"></param>
/// <returns></returns>
public static object GetSessionValue(string SessionKey)
{
object ReturnValue = null;
if (!string.IsNullOrEmpty(SessionKey))
{
ReturnValue = SessionHelper.GetValue(SessionKey);
}
return ReturnValue;
}
/// <summary>
/// Wrapper method of SetSessionValue suitable for MacroResolver.
/// </summary>
/// <param name="parameters">array of parameters</param>
/// <returns></returns>
public static object SetSessionValue(params object[] parameters)
{
switch (parameters.Length)
{
case 2:
return SetSessionValue(ValidationHelper.GetString(parameters[0], ""), parameters[1]);
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Sets a session value based on the session key
/// </summary>
/// <param name="SessionKey"></param>
/// <param name="SessionValue"></param>
/// <returns>Returns true of successful</returns>
public static bool SetSessionValue(string SessionKey, object SessionValue)
{
bool ReturnValue = false;
if (!string.IsNullOrEmpty(SessionKey))
{
SessionHelper.SetValue(SessionKey, SessionValue);
ReturnValue = true;
}
return ReturnValue;
}
}
}