Checking Session variable in Kentico web part "View" field

David Koukourou asked on September 6, 2016 07:52

How do I check if a session value exists in the view field of a web part? Also how can I get its value.

I tried the following: the session I am looking for is "postcode". It should be added before the page that has the webpart. {% postcode != null %}

not sure if thats the right syntax though as I don't know how to access the session variables.

thanks David K

Correct Answer

Richard Sustek answered on September 6, 2016 08:16

Kentico K# macros cannot retrieve session values (that wouldn't be very safe), but you can create your own macro to return a session value.

An example of the macro class:

using System;
using CMS.MacroEngine;
using CMS.Helpers;
using CMS;

// Makes all methods in the 'SessionMacroContainer' container class available for string objects
[assembly: RegisterExtension(typeof(SessionMacroContainer), typeof(string))]
// Registers methods from the 'CustomMacroMethods' container into the "String" macro namespace
[assembly: RegisterExtension(typeof(SessionMacroContainer), typeof(StringNamespace))]
public class SessionMacroContainer : MacroMethodContainer
{
    [MacroMethod(typeof(string), "Gets session value", 1)]
    [MacroMethodParam(1, "value", typeof(string), "SessionName")]
    public static string GetSessionValue(EvaluationContext context, params object[] parameters)
    {
        switch (parameters.Length)
        {
            case 1:
                var sessionName = ValidationHelper.GetString(parameters[0], null);

                return ValidationHelper.GetString(SessionHelper.GetValue(sessionName), String.Empty);

            default:
                // No other overloads are supported
                throw new NotSupportedException();
        }
    }
}

In ideal case you should also use your own Namespace instead of the "String" namespace that I'm using for simplicity. Then you could also hardcode the session name so that users cannot access sessions they shouldn't have access to (if necessary).

To use this code simply create class in your App_Code folder or Old_App_Code/class library in case of web apps.

You can call it like:

{% String.GetSessionValue("MySession") |(identity)GlobalAdministrator%}

1 votesVote for this answer Unmark Correct answer

Recent Answers


Kristian Bortnik answered on September 6, 2016 08:11

Out of the box, Kentico does not provide a way to access session data through macros.

You could write a custom macro method to accomplish this. Check out the documentation

In your custom macro method, you could access the session data using SessionHelper from the CMS.Helpers namespace.

var postcode = ValidationHelper.GetString(SessionHelper.GetValue("postcode"), string.Empty);

postcode would then be string.Empty if the session value is null.

1 votesVote for this answer Mark as a Correct answer

David Koukourou answered on September 6, 2016 08:20

Thanks guys,

I did exactly that and it worked really well. At least there is an example online now :)

Cheers David K

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.