Click or drag to resize

How To create application events

This article explains how to create and subscribe to application events.

These make inter-application communication easy for developers.

Note Note

There are other ways to make two application communicate.

For example, applications data and UI can be connected using our relation system without any line of code.

Trigger an application event

To trigger an application event, you have to call the method "TriggerEvent" on a PFApplication object.

Example: on the object class (Document.Custom.cs in the Objects folder), we trigger an event for all applications each time a document has been saved.

C#
protected override void OnInitialized()
{
    base.OnInitialized();

    this.AfterSave += Document_AfterSave;
}

void Document_AfterSave(object sender, PFItem_OperationEventArgs e)
{
    //Initialize event settings
    PFApplicationEvent myEvent = new PFApplicationEvent
        {
            Name = "DocumentSaved",
            Scope = PFApplicationEventScope.AllApplications
        };

    //Add custom parameters
    myEvent.Parameters.Add("Message", String.Format("The document {0} has been saved.", this.__Title));
    myEvent.Parameters.Add("Document", this);

    //Trigger event
    this.ParentApplication.TriggerEvent(myEvent);
}

The scope determines which applications will receive the event and can be chosen among the following values:

  • CurrentApplication: only the source application.
  • SpecificApplications: the list of applications specified in the SpecificApplications property of the PFApplicationEvent object.
  • AllApplications: all enabled applications of the PFSite.
  • AllApplicationsExceptCurrent: all enabled applications of the PFSite, except the source application.
Subscribe to application events
  1. Open your Visual Studio project.

  2. In the Objects\Application folder, open the Application_X_Events class.

  3. In this class, override the "Application_EventTriggered" method:

C#
public override void Application_EventTriggered(PFApplication sourceApplication, string eventName, Dictionary<string, object> parameters)
{
    base.Application_EventTriggered(sourceApplication, eventName, parameters);

    switch(eventName)
    {
        case "DocumentSaved":
            Document document = parameters.TryGetValue("Document") as Document;
            String message = parameters.TryGetValue("Message") as String;

            //Handle event here.

            break;
        default: break;
    }
}

Generic events

Other events can be overridden in the application events class:

  • Application_Installed: called when the application has been installed for the first time on the PFSite.
  • Application_Updated: called when the application has been updated.
  • Application_Enabled: called when the application has been enabled.
  • Application_Disabled: called when the application has been disabled.
  • Item_Created: called when an item has been created on the application.
  • Item_Updated: called when an item has been updated on the application.
  • Item_Deleted: called when an item has been deleted on the application.