3 Replies Latest reply on Jul 15, 2013 5:36 PM by mhamel

    How to get child elements that are weak references

    MaxStrueverWipro

      Hi, 

       

      I have an AF db with many Weak References. The weak reference of one element to another basically acts as a shortcut to find the referenced element, and because the relationship can be n:n, I chose weak references (let me know if this is not a good use of weak references).

       

      2318.Untitled.png

       

      I am not able to see the children elements that are weakly referenced via the AFSDK.

       

       

       
       AFElement plantEl = Master.AFdb.Elements["Plant ID 0"];
      plantEl.HasChildren; //shows up as 'true'
      plantEl.Elements.Count;  //shows up as 0, so I am not able to get the referenced elements 
      

       the .Elements property seems to only return parent-child referenced child elements, and not weak reference child elements. 

       

      What am I missing here? How can I access weakly referenced elements?

       

      using afsdk 2.4

        • Re: How to get child elements that are weak references
          xwang

          Hi,

           

          I got the different result from you, please see the following as the explanation:

           

          1. I did a Weak Reference and a Parent-Child Reference element under "Element2":

           

           

          2. I made codes to get the result of how many sub-elements in "Element2"

           

           

           
          class Program
              {
                  static void Main(string[] args)
                  {
                      PISystems afsdk = new PISystems();
                      PISystem afserver = afsdk["WIN-01ARJPPGTEJ"];
                      afserver.Connect();
                      AFElement my_element = afserver.Databases["training"].Elements["Element2"];
            
                      Console.WriteLine("the number of the sub-element is: {0}", my_element.Elements.Count);
                      Console.Read();
                  }
          

           3. The result is:

           

           

           

           

          You could see the number of the sub-elements is 2, which means you could get the access into the sub-element with the Weak Reference.

           

          4. In addition, I did another Weak Reference sub-element under the previous Weak Reference sub-element:

           

           

          5. I modified my codes as following:

           

           

           
          class Program
              {
                  static void Main(string[] args)
                  {
                      PISystems afsdk = new PISystems();
                      PISystem afserver = afsdk["WIN-01ARJPPGTEJ"];
                      afserver.Connect();
                      AFElement my_element = afserver.Databases["training"].Elements["Element2"].Elements["Element1"];
                      
            
                      Console.WriteLine("the number of the sub-element is: {0}", my_element.Elements.Count);
                      Console.Read();
                  }
          

           

           

           6. The result is following:

           

           

           

           

          From the above, I believe you could access all sub-elements, in spite of their upper element being Weak-Reference with its upper element also.

           

          Xi Wang

           

          v-Campus engineer

           

           

           

           

            • Re: How to get child elements that are weak references
              MaxStrueverWipro

              Ah you're right that worked.

               

              When I did a foreach of the .Elements, it showed the weakly referenced elements.

               

              I wonder why the count was misleading me.

               

              Thanks!

                • Re: How to get child elements that are weak references
                  mhamel

                  @Max: It is possible to have the HasChildren property to be true, but the Count one set to 0. Three scenarios as described below might have happened.

                  • One case would be if the user who is viewing the element does not have rights to view any of the children.
                  • Another case might be if the child element(s) have become obsoleted (setting Obsolete date in Version tab).
                  • A final mechanism might be if the user A creates and applies the new child element, but never checks it in. User B would not see the new element, because it is just in user A’s sandbox, but may see HasChildren set to true.