Using custom logic (script)

Use this component to perform any type of processing that is specified by a script. You can write your script to perform any document content and metadata operations, using C# or VB.NET. This feature stores information, extracts additional metadata, uses external data sources like databases, and so on. Both the input and output properties depend on the script.

If you are using Python scripting for processing, you must use the Python scripting component instead. For more information, see Using Python script capabilities.

Limitations

Note the following limitations with the script component:

  • AutoClassifier does not store or expose API keys, access keys, or other secrets in pipeline stages and components. If you need to retrieve your credentials in the script component, you must do so using Azure Key Vault storage. For more information, see Configuring your Azure key vault storage in the Configure your configuration settings topic.

  • The script component can not execute or spawn any external processes. Only importing libraries is permitted within the script component.

Configure your component

  1. In the AutoClassifier administration panel, Add a new component to a new or existing pipeline.
  2. When adding your component, select Script from the New Component list and provide a name for your component.
  3. In the Configuration section, expand the General Settings.
  4. To use the scripting pane, click the See the gear icon. icon to open the script settings window.

  5. Compile: Click Compile to compile your script.
  6. To configure:

    1. C#/VB: Use the default C# or select VB scripting.

    2. Change default script to generate a relative path and file name. Optional.

      1. Input Properties: Based on configuration.

      2. Output Properties: Based on configuration.
        You can write scripts for features and in multiple places for AutoClassifier. However, the script editor usage is the same for each location.

        Note: For this reason, although the scripting UI is different for features, the code examples are the same examples that are used in the Configure triggers using scripts section above.
  7. Add the component using the Add Components as Pipelines topic.

  8. To reset the partition size, go to Configuration > Profiling > Binary Data Extraction. Optional.
  9. Existing Components: Click the named link for this component to see the Configuration section:
  10. Type in the script.
  11. Compile: Click to verify the validity of the script.
  • See the following scripting examples:

Scripting Examples

Get an item property

To get an item property, use the generic GET method on item object:

C#
string propertyValue = item.Get<string>("propertyName");
VB
Dim propertyValue As String = item.[Get](Of String)("propertyName")

Get all item properties

To get all item properties, Use the GetAllMetadata method on an item object. This method accepts two parameters; a boolean to include or exclude the item body, and a format parameter to specify JSON or XML format for the output.

C#
Copy
//JSON 
string allMetadata = item.GetAllMetadata(true,"json");  // includes body and output in json format 
Log.Debug( " Body included and format is json "  + allMetadata );

string AllMetadataWithoutBody =  item.GetAllMetadata(false,"json");  // exclude body and output in json format 
Log.Debug( "Body excluded and format is json "  + AllMetadataWithoutBody );

//XML Format output 

//Note the xml format output is not visible in pipeline testing log display , please check the log files .

string allMetadataasxml = item.GetAllMetadata(true,"xml");  // includes body and output in xml format 
Log.Debug( " Body included and format is xml "  + allMetadataasxml );

string AllMetadataWithoutBodyasxml =  item.GetAllMetadata(false,"xml");  // exclude body and output in xml format 
Log.Debug(" Body excluded and format is xml "  + AllMetadataWithoutBodyasxml );



//Default as json output
string allMetadatadefault = item.GetAllMetadata(true);  // includes body , as no format input is specified , output is defaulted to json format
Log.Debug( " Body included and  default format is json "  + allMetadatadefault );
VB
Copy
' JSON Output 
Dim allMetadata As String = item.GetAllMetadata(True, "json") ' includes body and output in json format
Log.Debug(" Body included and format is json " & allMetadata)
    
Dim AllMetadataWithoutBody As String = item.GetAllMetadata(False, "json") 'excludes body and output in json format
Log.Debug("Body excluded and format is json " & AllMetadataWithoutBody)
    
'XML output
'Note the xml format output is not visible in pipeline testing log display , please check the log files .
    
Dim allMetadataasxml As String = item.GetAllMetadata(True, "xml")  'includes body and output in xml format
Log.Debug(" Body included and format is xml " & allMetadataasxml)
    
Dim AllMetadataWithoutBodyasxml As String = item.GetAllMetadata(False, "xml") 'excludes body and output in xml format
Log.Debug(" Body excluded and format is xml " & AllMetadataWithoutBodyasxml)
    
'Default is json 
Dim allMetadatadefault As String = item.GetAllMetadata(True)  'includes body , as no format input is specified , output is defaulted to json format 
Log.Debug(" Body included and  default format is json " & allMetadatadefault)

Note: To reduce the size of the output list, you can specify false in the scripts above. This excludes the body metadata element from being included in the property list. If you specify true, the body mettadata element will be included in the property list.

Set property values

Property values can be changed in a similar way by the generic SET method:

C#
item.Set<string>("propertyName", "propertyValue");
VB
item.[Set](Of String)("propertyName", "propertyValue")

Note: If you want to generate / handle multi-valued metadata via scripting, it is recommended to use List in favor of Array.

List<T> should be the type used for multi-valued metadata.

Retrieve item raw data

Item raw data: Raw data can be retrieved as a byte buffer using the RawData property.

C#
byte[] content = item.RawData;
VB
Dim content As Byte() = item.RawData

Retrieve secrets from your Azure Key Vault

AutoClassifier allows you to retrieve your credentials from Azure Key Vault. This prevents sensitive information from being included or exposed in your AutoClassifier scripts. You can retrieve your secrets by creating a KeyVaultUtility object, then calling the GetAzureSecretByKey method.

C#
KeyVaultUtility keyVault = new KeyVaultUtility();
keyVault.GetAzureSecretByKey("apikey");
VB
Dim keyVault As KeyVaultUtility = New KeyVaultUtility()
keyVault.GetAzureSecretByKey("apikey")
In the scripts above, you must replace "apikey" with the name of your API key as specified in Azure Key Vault. For more information, see Apps, API Keys, and Azure Key Vault secrets in the Microsoft documentation.

Log Entries

Logging: Use static methods to write entries into the log at different levels:

C#
Copy
Log.Debug("Debug message");
Log.Info("Info message");
Log.Warning("Warning message");
Log.Error("Error message");
VB
Copy
Log.Debug("Debug message");
Log.Info("Info message");
Log.Warning("Warning message");
Log.[Error]("Error message");

Define the assemblies and namespaces to import

  1. Click the See the gear icon. icon to define the namespaces to use.

  2. If you implement extra code in your script that calls third-party APIs, you will need to add the namespaces for the necessary libraries in the Namespaces textbox.

  3. Add all the Assemblies (DLL files) you wish to use in your scripts to the Admin Site\Config\ScriptingAssemblies folder from your AutoClassifier Engine install folder.

  4. If you upgrade from a version earlier than 6.1.0.0 - 18995 you must copy to \Config\ScriptingAssemblies all the previously referenced assemblies in your scripts and triggers. 
  5. For all the namespaces you want to use, add them in the pop-up text box and separate each namespaces with a new line. 

For example:

Copy
System.Text.RegularExpressions
System.Text
System.Linq
System.Collections.Generic
Newtonsoft.Json