Page 1 of 1

Get Name of Plugin Dynamically

PostPosted: Tue Feb 06, 2018 3:53 pm
by SBarnes
Is it possible to get the name of a plugin dynamically in code so that that you can get the value and not have to hard code it to retrieve system settings values, this is mainly for Form plugins and Business Logic Plugins?

Re: Get Name of Plugin Dynamically

PostPosted: Tue Feb 06, 2018 4:21 pm
by Mike.Sheen
Hi Stuart,

Yes!.

The Setup classes FormPlugin and BusinessLogicPlugin do have a parameter - Plugin which has a property Name which tells you which plugin the Setup method is being invoked for - usually in the Setup method you're usually adding event handlers - you just need to use a delegate to provide a handler, and then that delegate will have access to the variables / parameters within the scope of the Setup method - including the Plugin:

Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
   JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm salesOrderForm = (JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm)JiwaForm;
   
   salesOrderForm.SalesOrder.CreateEnd += delegate(object sender, System.EventArgs e)
   { 
      JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder = (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)sender;
      salesOrder.Manager.DisplayMessage(salesOrder.Client.Form, String.Format("This is Invoice No. '{0}' invoked from plugin '{1}'", salesOrder.InvoiceNo, Plugin.Name), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1);
   };
}   


Sample plugin for 7.1 attached.

Plugin Test plugin name access.xml
Sample Plugin
(32.18 KiB) Downloaded 99 times


Mike

Re: Get Name of Plugin Dynamically

PostPosted: Tue Feb 06, 2018 6:21 pm
by Mike.Sheen
To add to this, if you're like me and don't like bundling all your logic for the handlers as delegate functions within the Setup method, create a new method with the Plugin as a parameter, and invoke that from the delegate in Setup:

Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
   JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm salesOrderForm = (JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.SalesOrderEntryForm)JiwaForm;
   
   salesOrderForm.SalesOrder.CreateEnd += delegate(object sender, System.EventArgs e)
   { 
        SalesOrder_CreateEnd(sender, e, Plugin);
   };
}

public void SalesOrder_CreateEnd(object sender, System.EventArgs e, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
      JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder = (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)sender;
      salesOrder.Manager.DisplayMessage(salesOrder.Client.Form, String.Format("This is Invoice No. '{0}' invoked from plugin '{1}'", salesOrder.InvoiceNo, Plugin.Name), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1);
}


This way you can still separate the logic into methods outside the Setup method, making it tidier and avoiding everything in one monolithic lump of code.

Re: Get Name of Plugin Dynamically

PostPosted: Wed Feb 07, 2018 6:58 am
by SBarnes
Hi Mike,

Thanks for the replies, is there also an easy way to do this for other plugin types (CustomFieldPlugin, LineCustomFieldPlugin and SystemSettingPlugin)?

As I am trying to avoid hard coding this sort of thing in any type of plugin going forward.

Re: Get Name of Plugin Dynamically  Topic is solved

PostPosted: Thu Feb 08, 2018 6:27 pm
by Mike.Sheen
Hi Stuart,

For the CustomFieldPlugin and LineCustomFieldPlugin classes, each method (FormatCell, ReadData, ButtonClicked) has a parameter CustomField which you can obtain the plugin name from :
Code: Select all
CustomField.PluginCustomField.CustomFieldCollection.Plugin.Name


For the SystemSettingPlugin, a parameter SystemSetting can be used to also get the Plugin name - which is actually the Section name of the system setting:
Code: Select all
SystemSetting.SettingCollection.Section


Mike

Re: Get Name of Plugin Dynamically

PostPosted: Thu Feb 08, 2018 6:45 pm
by SBarnes
Thanks Mike.