Wiki

Case Status Kiln
Register Log In

Wiki

 
Interact with other plugins us…
  • RSS Feed

Last modified on 1/2/2015 1:39 PM by User.

Tags:

Interact with other plugins using CAllPluginsApi

Warning: CAllPluginsApi will fail if your plugin and the other plugin are running in separate app domains. This will be the case with a default installation of FogBugz if your plugin wishes to query fields in Custom Fields, for example. See this article for an alternative (and easier) way to get and set another plugin's bug-joined fields.


Sometimes a FogBugz plugin needs to know about, or interact with, other plugins. In those cases, you can use CAllPluginsApi to detect and invoke other FogBugz plugins.

We'll use the FogBugz Reporting Plugin as our example.

Finding a specific plugin

The FogBugz Reporting plugin needs case history data in a different (and more query-performant) format than core FogBugz provides in order to generate over-time data. So it uses another plugin, CaseHistory, to create, backfill, and maintain this data. This is neat; now other plugins can access CaseHistory's data just like Reporting does.

ICaseHistory caseHistory = api.AllPlugins.PluginFromId("CaseHistory@fogcreek.com") as ICaseHistory;

On calls to PluginFromId, FogBugz will search all of the currently installed plugins for one with an ID that matches, and return it to the caller as a FogCreek.FogBugz.Plugins.Plugin object which can then be cast to make calls on the desired interface.

Creating a custom plugin interface

The Reporting plugin provides a framework for showing metric on FogBugz cases, and we wanted to provide an interface for other developers to create custom metrics. To do that, the reporting plugin needs to create its own custom interface, and look for other plugins that implement that interface. 

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
private CMetric[] GetAllMetrics()
{
  List<CMetric> metrics = new List<CMetric>();

  foreach (Plugin plugin in api.AllPlugins.PluginsForInterface("IReportingMetrics"))
  {
    IReportingMetrics pluginMetrics = plugin as IReportingMetrics;
    metrics.AddRange(pluginMetrics.Metrics());
  }

  metrics.Sort();
  return metrics.ToArray();
}

With this in place, we can create a plugin-to-a-plugin! And that's exactly how the 'Reporting - Additional Metrics' plugin works. It implements IReportingMetrics, and acts as a plugin to the Reporting plugin.

Getting all fields from the CustomFields plugin (from another plugin)

Please see the warning at the top of this article. Another method for getting another plugin's fields is here.

It's now easy to get all of the Custom Fields defined by the user, even from a plugin other than CustomFields!

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
private string AllCustomFields()
{
	//reference PluginUtils\bin\FogCreek.Plugins.BugField.dll to get CBugFieldPlugin
	StringBuilder sb = new StringBuilder();
	FogCreek.Plugins.BugField.CBugFieldPlugin pluginCF = 
		(FogCreek.Plugins.BugField.CBugFieldPlugin) api.AllPlugins.PluginFromId("CustomFields@fogcreek.com");

	if (pluginCF == null) return "The Custom Fields plugin is not installed";

	foreach (FogCreek.Plugins.BugField.CBugField field in pluginCF.BugFields())
	{
		sb.Append(HttpUtility.HtmlEncode(field.sName + ": " + field.sDescription) + "<br />");
	}
	return "The Custom Fields plugin is installed, and its fields are:<br /><br />" + sb.ToString();
}