7 Replies Latest reply on Oct 29, 2012 10:08 PM by Ahmad Fattahi

    Programmatically Create Delivery Format Notification

    ericL

      Hi everyone,

       

      I have a requirement to create a custom email that will have some information specific to each individual notification that will come from the database. Based on this I think I need to create a Delivery Format for each notification.

       

       

       

      See this screenshot to understand. I used PI system explore to create a custom (email) Delivery Format that will be applied to that notification only (each notification will have its own delivery format email):

       

      3301.notification1.jpg

       

       

       

      ... then I will apply that template to the subscription:

       

      0572.notification2.jpg

       

       

       

      My main issue which I would like help with is on how to create the Email Delivery Format for a notification (screenshot 1).

       

      In C# I kind of guess that I would use the method Notification.DeliveryFormats.AddFormat(), is this right? Also I would like to get if possible an example on how to do this:

       

      var notification = new AFNotification(afDatabase, "NotificationName");
      var configNotific = new ANConfigurationNotification(notification);
      configNotific.DeliveryFormats.AddFormat(....)

       

      Thanks,

       

       

        • Re: Programmatically Create Delivery Format Notification
          Ahmad Fattahi

          Eric,

           

          This thread seems to address the exact same issue you are having here. Also, chapter 9 of the PI Application Development Training Course work book (found in vCampus Training Center) goes over programmatic access to PI Notifications in detail with lots of examples.

            • Re: Programmatically Create Delivery Format Notification
              ericL

              Thanks Ahmad, although I looked in the thread you mentioned and also chapter 9 of the PI Application Development Training Course but I didn`t find what I was looking for.

               

              Basically what I want is to programmatically create a Delivery Format with custom email body and subject  for an individual notification because the email body text will have dynamic data that email templates like the Global Default Email found in the Notification Settings can`t be used.

               

              I think the method below Notification.DeliveryFormats.AddFormat() might be what I want but I haven`t found any examples on how to do that.

               

              var notification = new AFNotification(afDatabase, "NotificationName");

               

              var configNotific = new ANConfigurationNotification(notification);

               

              configNotific.DeliveryFormats.AddFormat(....)

               

               

               

              Please let me know if you have any examples on how to create a custom Delivery Format email (setting the body and subject of the email) for a notification (screenshot 1 in the first message).

               

              Thanks,

                • Re: Programmatically Create Delivery Format Notification
                  Ahmad Fattahi

                  Below is an example of creating a format. You can also check out PI Notifications Developer Tools in vCampus Download Center under Analytics.

                   
                  ' Add a format 
                  Dim context As New ANConfigurationNotificationTemplate(myNotificationTemplate)
                  context.EnableFormatting() ' If not enabled DeliveryFormats will return null. 
                  Dim format As ANDeliveryFormat = AddFormat(templateFormatName, context, True)
                  Console.WriteLine("The format has been successfully created.")
                  
                  ' Add a subscriber and set it to use the new format
                  Dim contactTemplate As AFNotificationContactTemplate = CreateOrGetContactTemplate()
                  Dim contact As AFNotificationContact = myNotificationTemplate.NotificationContacts.Add(contactTemplate)
                  context.DeliveryFormats.SetSubscriberDeliveryFormat(contact, format)
                  Console.WriteLine("The notification has been successfully created.")
                  
                  
                  === The AddFormat fucntion is here; and "name" in the AddFormat call is the name of the format he created ===
                  
                  
                   Private Function AddFormat(ByVal name As String, ByVal notificationContext As ANConfigurationNotificationBase, ByVal addNewContent As Boolean) As ANDeliveryFormat
                  
                              Dim format As ANDeliveryFormat = notificationContext.DeliveryFormats.AddFormat(name, New Guid(DeliveryChannelGUIDs.Email))
                              format.Version = 1   ' This sample is written for email format version 1
                  
                              ' Add Importance
                              Dim key As String = "Importance"
                              Dim value As String = MailPriority.High.ToString()
                              format.Properties.Add(key, value)
                  
                              ' Add subject
                              ANXamlFormatProcessor.SaveConfiguration(format, "Subject", CreateDefaultSubject())
                  
                              ' Add contents to the body of the message
                              ANXamlFormatProcessor.SaveConfiguration(format, "Body", CreateBody(notificationContext, addNewContent))
                  
                              If addNewContent Then
                                    ' Attach a file
                                    AddFilesToFormat(format)
                              End If
                  
                              Return format
                        End Function
                  

                   

                    • Re: Programmatically Create Delivery Format Notification
                      ericL

                      Thanks for the example Ahmad.

                       

                      Just an extra explanation we would require.

                       

                      For the Body of the message, is it possible to add content that is stored within the AF attributes of the Target (i.e. Element)? I understand that we can customize standard text, but we are trying to populate the delivery format with specific information; this information that we'd like to add in the delivery format is found within that element, in AF attributes.

                       
                      ' Add contents to the body of the message
                      ANXamlFormatProcessor.SaveConfiguration(format, "Body", CreateBody(notificationContext, addNewContent))

                      From the line above, how can we add placeholders so that as it is created, the notification replaces these placeholders with the appropriate values coming from the AF attributes found within that element/target?

                       

                      For example,

                       

                      Scenario: Within one element, I have an AF attribute called EquipmentID, its value is: 12345

                       

                      What we need to do programmatically: create a notification with a new delivery format that includes the EquipmentID information.

                       

                      Essentially, when I create a notification programmatically, I consult my notification and see that a custom delivery format has been assigned to the notification and the email that is received may look something like:

                       

                      "This email is to alert you that the equipment with : EquipmentID: 12345 has a value out of threshold"

                       

                      If I created another notification programmatically based on another equipment, using the same code, it would change 12345 with the appropriate EquipmentID.

                       

                       

                       

                      Hope that is clear.

                       

                      Is this possible? Just trying to confirm whether it is as we need to give a Yes/No answer to the client. We want to see how flexible it is to add content coming from AF attributes.

                       

                       

                       

                      Thank you very much for your help. It is much appreciated.

                       

                       

                       

                       

                        • Re: Programmatically Create Delivery Format Notification
                          dmoler

                          Hi Eric,

                           

                          Delivery formatting was designed to solve exactly this problem.  The way to get dynamic content is to create a piece of content and add that content in the appropriate place in your email message format.

                           

                          There are a few different ways to approach this.  If you have a Notification Template defined for the notifications in question, you can add the EquipmentID AttributeTemplate as content (in the latest release, attributes/attribute templates on the target are available automatically).  The advantage of configuring the delivery format on the template is that every notification instance will have access to it.  You can even make it the "default" so all email subscribers will receive it by default.  In the UI it looks like this:

                           

                          7220.Capture.PNG

                           

                           

                           

                          If you don't have the benefit of Notification Template but have enough notifications that you want to automate the configuration, then you can add this content programmatically.  You'll need to insert "ANInlineContent" objects into the Xaml document as described in the vCampus samples.  The ANInlineContent placeholder is replaced by the content property value when the alert is sent.

                           

                          I hope this is helpful to you.  Even if you end up needing to do this programmatically, I encourage you to configure it once in the UI so you can see how all the pieces will fit together.