C# Functions

Creating External C# Functions

An external C# function is a CMS function written in C# and created in an external code editor such as Visual Studio (unlike internal C# functions created directly in the CMS Console).

To create an external C# function:

  1. Create a C# method in a code editor of your choice (for example, Visual Studio) and move it to the website.
  2. Add this method to C1 CMS as an external C# function in the CMS Console.

Please note that you can:

  • Keep your C# method in a .cs file in the /App_Code folder. (You may want to create /App_Code as there might be no /App_Code in C1 CMS initially.)
  • Build a class library with this method and place it in the /Bin folder.

Please also note that on pages you can use C# functions when switched to the Source view. To learn to make them available in Visual Editor, too, please see “Integrating with Visual Editor”.

Creating C# Method

To create the C# method in a .cs file in /App_Code:

  1. In your code editor, create a C# class in the website’s App_Code folder (or elsewhere and then move it to App_Code).
  2. Add using statements and references if required in your code.
  3. Add namespaces if necessary.
  4. Make sure the class is public.
  5. Create a public static method in this class.
  6. Save the .cs file.
using System;
namespace Demo
{
  public class Functions
  {
    public static string CurrentDate()
    {
      return DateTime.Now.ToLongDateString();
    }
  }
}

Listing 4: Demo.Functions.CurrentDate method

C1 CMS must become aware of this newly added class and all its public static methods.

Please note that the file should be placed in the App_Code folder or one of its subfolders. Both the class and its method(s) must be public and the method must also be static.

You can create as many classes in one file and methods in one class as you need.

If you need to return XHTML from a function, consider using XhtmlDocument.

Alternatively, you can create a class library with the method:

  1. In Visual Studio, create a class library project.
  2. Add using statements and references if required in your code.
  3. Add namespaces if necessary.
  4. Make sure the class is public.
  5. Create a public static method in this class.
  6. Build the code.
  7. Copy the output DLL to the /Bin folder of your website.

Adding Description

A regular CMS function can have a description. To provide a description for an external C# function, use the Function attribute on the C# method setting its Description parameter to user-friendly text:

using System;
namespace Demo
{
  public class Functions
  {
    [Function(Description="Returns the current date in long format")]
    public static string CurrentDate()
    {
      return DateTime.Now.ToLongDateString();
    }
  }
}

Listing 5: C# method with some description

Adding Parameters

You can add parameters to such C# methods.

using System;
namespace Demo
{
  public class Functions
  {
    public static string ShowSomeInfo(string text, int number)
    {
      return text + ": " + number.ToString();
    }
  }
}

Listing 6: C# method with parameters

A regular CMS function can have a user-friendly label, some help text, a default value and a widget other than the default one. They appear when you use the function.

You can provide these settings for external C# functions as well - by using the FunctionParameter attribute on the C# method:

using System;
using Composite.Functions;
namespace Demo
{
  public class Functions
  {
    [FunctionParameter(Name = "text",
                       Label = "Some Text",
                       Help = "The text to show",
                       DefaultValue = "Amount",
                       WidgetMarkup = 
@"<f:widgetfunction name='Composite.Widgets.String.TextArea' xmlns:f='http://www.composite.net/ns/function/1.0' />")]
    [FunctionParameter(Name = "number",
                       Label = "Some Number",
                       Help = "The number to show",
                       DefaultValue = 5)]
    public static string ShowSomeInfo(string text, int number)
    {
      return text + ": " + number.ToString();
    }
  }
}

Listing 7: Using the FunctionParameter attribute

As you can see in the example above, in the FunctionParameter attribute, you need to specify the name of the parameter in the Name parameter, and the needed settings in the following parameters:

  • Label
  • Help
  • DefaultValue
  • WidgetMarkup

You can provide the widget markup via the <f:widgetfunction> element specifying its name and its namespace. If the widget have parameters, you can specify the parameters, too, via the <f:param> element:

[FunctionParameter(Name = "text", WidgetMarkup =
@”<f:widgetfunction name="Composite.Widgets.String.TextBox" xmlns:f="http://www.composite.net/ns/function/1.0"><f:param name="SpellCheck" value="False" /></f:widgetfunction>”)]

Listing 8: Using the widget with parameters

Note: The FunctionParameter attribute is available in Composite C1 (now C1 CMS) version 3.2 or later.