Execute Command

If you know C# the world is now your oyster! You can now use those skills to make rich and powerful Lava templates. For this tutorial we'll start simple and work our way to more sophisticated script blocks.


This tool is like a loaded gun. There is no rescuing from stack overflow exceptions and whilst you will be protected from some exceptions always assume that you won't be.

Hello World

Where else would we start, but 'Hello World'?

{% execute %}
    return "Hello World!";
{% endexecute %}

As you probably inferred, anything inside the execute tags is treated as C#. Your scripts must return a single string that then becomes the output.

Lava Generated C# That Renders As Lava

What!? So get this... Your C# script will be processed as a Lava template, run as C# all inside a Lava template. Clear as mud? This simple example will help.

{% execute %}
    return "{{ Person.FullName }}";
{% endexecute %}

See that? Consider the Inception-like logic of this one...

{% capture personScriptResults %}
    {% execute %}
        return "{{ Person.FullName }}";
    {% endexecute %}
{% endcapture %}

{{ personScriptResults }}

While this example is a simplistic use case, we hope you see the patterns that you can achieve.


You've probably noticed that this is basic C#. So far we haven't seen any classes, methods, etc. You might be wondering what you do when you need to reference a class that's in another assembly. Normally, you'd use an import to configure the assembly. You can add imports using the 'import' paramater. Below is a quick script that grabs the last Github commit from the Rock repo.

{% execute import:'RestSharp,Newtonsoft.Json,Newtonsoft.Json.Linq' %}
    var client = new RestClient( "https://api.github.com" );
    var request = new RestRequest("repos/SparkDevNetwork/Rock/commits", Method.GET);
    IRestResponse response = client.Execute(request);

    JArray commitArray = JArray.Parse(response.Content);

    dynamic firstCommit = commitArray.First["commit"];
    return firstCommit.message + "<br />" + firstCommit.author.name;
{% endexecute %}

Yes, we know the code above is a bit of a hack. It was built for brevity and to show a few specific features. Notice that we added two assemblies in the import.

To keep you from having to add too many imports, we auto-import the following assemblies for you. (You're welcome.)

  • System
  • Rock
  • Rock.Model
  • Rock.Data

Full Class

OK you C# geniuses... you can use full classes if you want to.

{% execute type:'class' %}
    using Rock;
    using Rock.Data;
    using Rock.Model;
    public class MyScript 
        public string Execute() {
            using(RockContext rockContext = new RockContext()){
                var person = new PersonService(rockContext).Get({{ CurrentPerson.Id }});
                return person.FullName;
{% endexecute %}    

Take a closer look at that class. It pulls up the Person model for person in the context of the page.

You don't have to import, but when you do, don't import System. You may be tempted to import the System assembly. This can cause some issues for you and we recommend that you don't. The way the script is run you will not need it.