2 Replies Latest reply on Jan 25, 2018 3:54 PM by Asle Frantzen

    Update template programmatically without losing attribute values?

    Asle Frantzen

      Hi

       

      I'm writing an application (PI AF SDK) where a user should be able to store the exported xml for an element template, keep it for later, and then re-import it into the AF Server as a backup.

       

      I would like to retain elements derived from the template, and their values, just like you do in PI System Explorer if you change template and the attribute templates also exist in the destination template. The attributes must keep their values, I don't want them reset to whatever is specified in the template.

       

      Example:

       

      Original element template

      Attribute1

      Attribute2
      Attribute3

      +

      Modified element template
      Attribute1
      Attribute2
      Attribute4

       

      I've tried to simply import the XML into the same element template, but this only adds content which was missing - it doesn't remove attribute templates from AF. See the two tables. If I import the XML of the original template into the modified template I end up with this:

      Resulting template

      Attribute1

      Attribute2
      Attribute3
      Attribute4

       

      This is not what I want. I want to go back to the state of the original template. I figure I need to keep editing the template in question, in order to keep derived elements and not lose attribute values, so I tried clearing the collection of attribute templates before importing. Now the attribute template collection is the same as in the original template, but I didn't retain the attribute values of the derived elements. Attribute2 had a manually entered string value in one of my elements, and this was reset to the value specified in the template. So I'm a bit stuck here now.

       

      Any recommendations?

        • Re: Update template programmatically without losing attribute values?
          gregor

          Hello Asle,

           

          I have tried reproducing what you've been doing and my observation is that as soon as you modify the Attribute Templates of an Element Template, there is impact to the Attributes of derived Elements. With other words if you remove an Attribute Template you cause the corresponding Attribute to be removed from derived Elements. When you add an Attribute Template, the corresponding Attribute will be added to Elements derived from your Element Template and being initiated with the value specified in the Template.

           

          The only way to implement the logic you have in mind is to see what Attributes exist with the current Element Template against what is specified in the XML export. You would keep those Attribute Templates untouched which exist in both, remove those which exist only in the current Template and add those which do not exist in the Template but in the XML file. However, there is always impact to the Attributes of derived Elements.

            • Re: Update template programmatically without losing attribute values?
              Asle Frantzen

              Thanks Gregor!

               

              I discussed this with a couple of colleagues today as well, and we came to the same conclusion.

               

              It would mean that I need to dig deeper into the XML, by using more specialized XML comparers instead of what I originally planned to use - a simple string compare. But I feel this is a must-have if you're dealing with a production-database with lots of derived elements. (If this was purely a template-store database, we would probably be alright taking the easy way.)