Documentation Center

Creating a Write plugin in C#

This example shows how to create a C# .NET Write plugin that extracts title from the XML content and saves it to the FTITLE metadata field.

Before you begin

The following products need to be already installed on your machine:
  • Microsoft .NET FrameWork
  • Microsoft Visual Studio

Procedure

  1. Create a Visual Studio Project. In this example, the project is called MyCustomPluginLibrary, and its type is Class Library.
  2. Add a reference to a plugin SDK (Trisoft.InfoShare.Plugins.SDK.dll). For example, you can copy the file from \Applications\Common\Trisoft.InfoShare.Plugins.SDK.dll.
    1. Browse to the project, then right-click References, and select Add Reference.
    2. To select the reference you want to add, use the Browse button.
    3. Select Trisoft.InfoShare.Plugins.SDK.dll, then Add.
    4. To be able to view the code documentation of the Trisoft.InfoShare.Plugins.SDK.dll, also copy Trisoft.InfoShare.Plugins.SDK.xml.
  3. Add a reference to a plugin common helper (Trisoft.InfoShare.Plugins.Common.dll). For example, you can copy the file from \Applications\Plugins\Trisoft.InfoShare.Plugins.Common.dll.
    1. Browse to the project, then right-click References, and select Add Reference.
    2. To select the reference you want to add, use the Browse button.
    3. Select Trisoft.InfoShare.Plugins.Common.dll, then Add.
    4. To be able to view the code documentation of the Trisoft.InfoShare.Plugins.Common.dll, also copy Trisoft.InfoShare.Plugins.Common.xml.
  4. Add a reference to the System.ComponentModel.Composition assembly.
  5. Add a reference to the System.Xml assembly.
  6. Create a new MyCustomPlugin class.
    The name of the plugin corresponds to the value assigned to ExportAttribute, and it needs to be unique.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using Trisoft.InfoShare.Plugins.SDK;
    
    namespace MyCustomPluginLibrary
    {
        // This attribute is used to make the class discoverable by the plugin engine.
        [Export("MyCustomPlugin", typeof(IWriteMetadataAndBlobPlugin))]
        [PartCreationPolicy(CreationPolicy.NonShared)]
        /// <summary>
        /// Custom plugin that will be triggered on write action in case when the blob is updated
        /// </summary>
        public class MyCustomPlugin : IWriteMetadataAndBlobPlugin
        {
            /// <summary>
            /// This variable is used to store the xpath used to select the node inside the context.
            /// </summary>
            private string _xpath;
    
            /// <summary>
            /// The configuration
            /// </summary>
            private IPluginConfiguration _configuration;
    
            /// <summary>
            /// This property will be set by the plugin engine
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            /// his property will be used by the plugin engine
            /// to determine how to pass the blob to the plugin.
            /// </summary>
            public ObjectContentType ObjectContentType
            {
                get
                {
                    // This plugin consumes the blob as an xml document.
                    return Trisoft.InfoShare.Plugins.SDK.ObjectContentType.XmlDocument;
                }
            }
    
            // This method will be called by the engine for every plugin after the instance of the plugin is created.
            public void Initialize(IPluginConfiguration configuration)
            {
                _configuration = configuration;
    
                // Read the value of the parameter that is expected in the plugin xml configuration
                if (!_configuration.Parameters.TryGetValue("NodeXPath", out _xpath))
                {
                    // If parameter was not provided, throw an exception
                    var exception = new InvalidOperationException("NodeXPath parameter was not provided");
                    _configuration.LogService.ErrorException(String.Format("Plugin '{0}' failed", this.Name), exception);
                    throw exception;
                }
            }
    
            // This method will be called by the engine as part of a main action
            public void Run(IWriteMetadataAndBlobContext context)
            {
                // Select a node from the content
                // Note that we use ObjectDocument property to get to the blob as xml
                // We use XmlNamespaceResolver property to make sure that the namespace prefix used in the xpath is resolved to a namespace
                XmlNode node = context.ObjectDocument.SelectSingleNode(_xpath, context.XmlNamespaceResolver);
    
                // Use SetStringFieldValue method to set the metadata field
                context.SetStringFieldValue("FTITLE", IshLevel.Logical, node.InnerXml);
    
                // Add a message to the logging pipeline
                _configuration.LogService.Info(this.Name + " plugin executed successfully.");
            }
    
            // This method will be called by the plugin engine after all plugins have executed
            public void Dispose()
            {
                // We don't need a cleanup
            }
        }
    }
  7. Deploy the new plugin.
  8. Configure the new plugin.
    context.LogService manages all log entries as part of the standard log chaining, as configured in NLog.config. If you want to create a separate logging chain, you need to set up a new Logger.
    <plugin name="MYCUSTOMPLUGIN" handler="MyCustomPlugin" ishcondition="EDT='EDTXML'">
      <description>My custom plugin</description>
      <workingset>
        <ishfields>
          <ishfield name="FTITLE" level="logical" />
        </ishfields>
      </workingset>
      <initialize>
        <parameters>
          <parameter name="NodeXPath">(/*[contains(@class,' map/map ')][title]) | (/*[contains(@class,' topic/topic ')][title])</parameter>
        </parameters>
      </initialize>
    </plugin>