C# Functions

Editing Inline C# Functions

Based on the template selected when creating the function, you get the initial source code in the source code editor.

It is now your task to implement the desired logic by editing the source code within the created method.

Besides, you can add one or more input parameters to the function.

You can keep track of the function’s work by previewing its current output (debugging) as you continue to edit it. Debugging can be configured to work in the environment your function is intended to be.

To edit an inline C# function:

  1. In the Functions perspective, expand C# Functions and expand namespaces the function belongs to.
  2. Select the function and click Edit on the toolbar. The function opens in the function editor.
  3. On the Settings tab, edit its general and debug settings.
  4. On the Source tab, edit the function’s source code.
  5. On the Assembly References tab, select assemblies, references to which are required by your source code.
  6. On the Input Parameters tab, add, edit or delete its input parameters.
  7. On the Preview tab, debug the function by previewing its return result.
  8. Click Save.

Editing General and Debug Settings

You normally set a function’s general settings when creating it. However, you can later change its general settings on the Settings tab of the function editor.

The general settings of an inline C# function include:

  • Name: The name of the function
  • Namespace: The namespace it belongs to
  • Description: The description of the function

Figure 4: Settings tab

Along with the general settings, you can manage the function’s debug settings here.

If you want to quickly see what this function will output without using it in the content yet, you can debug it by previewing it on the Preview tab.

The debug settings of an inline C# function include:

  • Page: The page used as the context for rendering when debugging
  • Data scope: The public or development version of data as the context for rendering when debugging
  • Language: The language to use for debugging

Editing Source Code

When an inline C# function has been created, it already has initial source code based on the selected template.

The code usually includes:

  • The list of used namespaces (using...)
  • The namespace this method belongs to (namespace Demo {...})
  • The system-generated public static class for inline C# methods (public static class InlineMethodFunction {...})
  • The public static method that serves as an inline C# function in C1 CMS (public static XElement Show3(...) {...})

Normally, you make your changes in the method.

Figure 5: Source tab

Please note that if you change the namespace and the name of the method in the source code, you should also change them on the Settings tab.

Please also note that you cannot change the class name. The class and the method should both be public and static.

When inserting the “using” statements, make sure you have included required assembly references.

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

Selecting Assembly References

The code you write in your inline C# function normally requires one or more assembly references.

Figure 6: Assembly References tab

When you create the function, the default set of assembly references is added. You can however choose to include or exclude references at will.

To manage assembly references:

  1. Edit an inline C# function.
  2. On the Assembly References tab, click Edit Selections.
  3. In the window that opens:
    1. move to the right list the assemblies the references to which you want to include
    2. move to the left list the assemblies the references to which you want to exclude

    Figure 7: Including and excluding assembly references

  4. Click OK and save the function.

Using Input Parameters

Input parameters enable users of an inline C# function to customize its behavior. The values specified in the input parameters can be further used in the source code.

Please note that if you add an input parameter in the source code as the parameter of the corresponding method, you must add the same parameter (name, type) on the Input Parameter tab and vice versa.

To add an input parameter to an inline C# function:

  1. Edit an inline C# function.
  2. On the Source tab, add a parameter to the method (for example, int myIntParam)

    Figure 8: Parameters added in source code

  3. On the Input Parameters tab, add the parameter with the same name and type. (For information on creating input parameters, please refer to the Guide to Function Parameters.)

    Figure 9: Parameters added on the Input Parameters tab

  4. Click Save.

Previewing Output Result

You can preview the result you inline C# function should return on the Preview tab.

Figure 10: Previewing the function’s return result

This is also a way of debugging your function. If something is wrong, the error will be displayed in the preview instead of the result.

Figure 11: Error displayed