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:
- In the Functions perspective, expand C# Functions and expand namespaces the function belongs to.
- Select the function and click Edit on the toolbar. The function opens in the function editor.
- On the Settings tab, edit its general and debug settings.
- On the Source tab, edit the function’s source code.
- On the Assembly References tab, select assemblies, references to which are required by your source code.
- On the Input Parameters tab, add, edit or delete its input parameters.
- On the Preview tab, debug the function by previewing its return result.
- 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
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.
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:
- Edit an inline C# function.
- On the Assembly References tab, click Edit Selections.
- In the window that opens:
- move to the right list the assemblies the references to which you want to include
- move to the left list the assemblies the references to which you want to exclude
Figure 7: Including and excluding assembly references
- Click OK and save the function.
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:
- Edit an inline C# function.
- On the Source tab, add a parameter to the method (for example, int myIntParam)
Figure 8: Parameters added in source code
- 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
- Click Save.
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