C# Functions
Adding External C# Functions Programmatically
It is possible to register a C# method as a C1 function from the code of your custom assembly so that it can be used in C1 CMS in markups or directly as a native C1 function by end users. You can use the default CodeBasedFunctionProvider to create one without creating your own function provider.
With this provider, you can easily register both static and instance methods of your custom class. Imagine your package has the following DataProvider class with the static method GetDataFromStaticMethod and the instance method GetDataFromInstanceMethod.
using System.Collections.Generic; namespace Orckestra.Example.Assembly { /// <summary> /// Example class /// </summary> public class DataProvider { /// <summary> /// Get data from a static method /// </summary> public static IEnumerable<string> GetDataFromStaticMethod() { return new [] { "data 1", "data 2" }; } /// <summary> /// Get data from an instance method /// </summary> public IEnumerable<string> GetDataFromInstanceMethod() { return new [] { "data 3", "data 4" }; } } }
Listing 9: Data Provider class
To register a static method as a C1 function you have to take the following steps for your assembly:
• Add the StartupHandler class, augmented with [ApplicationStartup] attribute (if it does not exist yet)
• In the OnInitialized() method, register the C# method as a C1 function, calling the RegisterMethod method of the CodeBasedFunctionRegistry class as in the example below.
public static void OnInitialized() { CodeBasedFunctionRegistry.RegisterMethod<DataProvider>( nameof(DataProvider.GetDataFromStaticMethod), "Parent.Category.ExampleStatic", "Description 1"); }
Listing 10: OnInitialized method
And that’s all. After you have installed your assembly as a part of a C1 package or even copied a compiled DLL to the bin folder of the website and restarted it, this method will become available as a C1 function.
Registering an Instance Method
To register an instance method, you should take the same steps as described above for a static method.
However, to avoid creating a new object on each method call (if it is not necessary) in the StartupHandler class in the ConfigureServices method, register a singleton for a desired type.
public static void ConfigureServices(IServiceCollection collection) { collection.AddSingleton(typeof(DataProvider)); }
Listing 11: Registering a singleton
In this case, the same object will be used with each GetDataFromInstanceMethod call. As a result, the StartupHandler class should look as below.
using Composite.Core.Application; using Composite.Plugins.Functions.FunctionProviders.CodeBasedFunctionProvider; using Microsoft.Extensions.DependencyInjection; using Orckestra.Example.Assembly; namespace Orckestra.Test.Assembly { [ApplicationStartup()] public static class StartupHandler { public static void OnBeforeInitialize() { } public static void OnInitialized() { CodeBasedFunctionRegistry.RegisterMethod<DataProvider>( nameof(DataProvider.GetDataFromStaticMethod), "Parent.Category.ExampleStatic", "Description 1"); CodeBasedFunctionRegistry.RegisterMethod<DataProvider>( nameof(DataProvider.GetDataFromInstanceMethod), "Parent.Category.ExampleInstance", "Description 2"); } public static void ConfigureServices(IServiceCollection collection) { collection.AddSingleton(typeof(DataProvider)); } } }
Listing 12: StartupHandler class
After you have copied a compiled assembly to the bin folder of the website and restarted it (for example, as part of the package installation), the GetDataFromStaticMethod and GetDataFromInstanceMethod methods will become available as C1 functions ExampleStatic and ExampleInstance.
Figure 12: Step 1: The methods available as C1 functions
The end user can call these registered C1 functions directly in the C1 CMS Console or use the functions in markups and other C1 CMS functionality.
Figure 13: Viewing the results of a function