Official Content

If you have to type the same code frequently, you can take advantage of the Code Snippets in any GeneXus object in order to reuse them as templates that can be activated easily via drag and drop or using a shortcut from the Snippets toolbox.

For example let's assume you are using the Work With pattern extensively and want to reuse the following code, which is automatically generated by the pattern to check the security, in an easy way:

PGetContext.Call(&Context)
IsAuthorized.Call(&PgmName, &IsAuthorized)
If (not &IsAuthorized)
    NotAuthorized.Call(&PgmName)
Endif

Suppose that instead of copying a pasting this code between objects and defining its variables, you want to have an easy way to insert the code in a Procedure. You can do this by using a code snippet, to encapsulate this code and use it as a command, like available ones such as Call, For Each command, For In, etc. 

Follow these steps to create a Code Snippet:

Step 1. Create a snippet file wrapping your desired code

Within the GeneXus X Evolution I installation directory, you will notice the default code snippets folder in the <GeneXusInstallationFolder>\Snippets folder.

The easiest way to create your own snippet is to reuse an existing one, and then do the desired modifications. This approach will be used.

Copy any command snippet and rename it, in this case its name will be Check Security command.snippet.

Code snippet - Saving as

Edit it and replace its content with the following code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Check Security Sample</Title>
            <Shortcut>css</Shortcut>
            <Description>Reuses the security checking generated by the Work With Pattern</Description>
            <Author>Artech</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="true">
                    <ID>ContextVariable</ID>
                    <ToolTip>Context SDT Variable</ToolTip>
                    <Default>&amp;Context</Default>
                    <Function>
                    </Function>
                </Literal>
                <Literal Editable="true">
                    <ID>IsAuthorizedVariable</ID>
                    <ToolTip>IsAuthorized Boolean Variable</ToolTip>
                    <Default>&amp;IsAuthorized</Default>
                    <Function>
                    </Function>
                </Literal>
            </Declarations>
            <Code Language="">
                <![CDATA[LoadContext.Call($ContextVariable$)
IsAuthorized.Call(&PgmName, $IsAuthorizedVariable$)
If (not $IsAuthorizedVariable$)
    NotAuthorized.Call(&PgmName)
Endif
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Some comments about the meaning of this code:

  • The Title, Shortcut and Description tags are important because they detail how you will see and use the code snippets in the IDE. In this case:
    • The Check Security Sample value will be shown as an available snippet.
    • When using the mouse over, you will get a tooltip with the css shortcut and the associated description.

                           Code snippet - Toolbox new item

  • Check the Code tag, which contains the desired block of code to be inserted. In this case the code has some minor changes comparing with the initial one because there are some variables to be replaced, that's why you will notice that the $ContextVariable$ and $IsAuthorizedVariable$ parameters are used. These are Literals which will have to be replaced.
  • Check the Declarations tag, containing the default values for these variables.

Step2. Register your snippet for the desired GeneXus Object

Your snippet file is ready, so you will have to register it in the desired GeneXus object you want to use.

To register your snippet in a GeneXus object you will have to add an entry in a XML File. Notice that for each GeneXus object there's a config.xml file detailing the available ones for each part.

Code snippet - Object Registration

In this case,  just register it in the Source Procedure section; notice that you need to add a new Snippet tag with the name of the snippet file within the <GeneXusInstallationFolder>\Snippets\Procedure\Procedure\config.xml configuration file:

<Snippets>
  <Category name="Snippets">
        <Snippet name="Check Security command"/>
        <Snippet name="Call command"/>
......

Ready; you have finished the registration process.

Step 3. Restart GeneXus

Make sure you have restarted your IDE.

Step 4. Check your new Snippet

To make sure your custom snippet for the Source Procedure section is available, open or create a new Procedure, and go to the snippet toolbox. Check the tooptip for more information.

Code snippet - Toolbox new item

Step 5. Use it !!

To use it just drag & drop it or type the shortcut (css for "Check Security snippet" in this example) and tab; you will get the desired replacement.

Code snippet - Using the new one

Once inserted, you just have to create the &Context and &IsAuthorized variables to complete the operation; notice the related green section.

Congratulations! You have created and used a custom GeneXus Code Snippet!

Note: Backup your Code Snippets every time you install a new GeneXus Upgrade.

Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant