1 Reply Latest reply on Jun 19, 2018 10:27 PM by Rick Davin

    AFElementSearch question

    Dava

      HI all,
      I'm new using the new AFElementSearch.
      And I have a question , that maybe is an easy one.
      I need to make a search to  know if any of the children for given element has a specific template.
      I've been trying without success:

       

      Any comment will be appreciate

      regards

        • Re: AFElementSearch question
          Rick Davin

          Hi David,

           

          While pictures are pretty, for future reference it would be nice if you could post the actual text.  This would save typing time for people who are trying to help you.

           

          Your code does nothing with variable rootToke2n.  The last line may be a typo because you issue the FindElements on a ChildrenSearch variable and not ChildrenSearch2.  The search classes have been disposable since AF 2.9.0.

           

          If both your client and server are on PI AF 2017 R2 (AF SDK 2.9.5), the Parent filter is supported.  I mention this because the help mentions how using Root can be very slow.

           

          Tokens

          IList<AFSearchToken> tokens = new List<AFSearchToken>();
          tokens.Add(new AFSearchToken(AFSearchFilter.Parent, AFSearchOperator.Equal, CurrentElement.GetPath(AFdB)));
          tokens.Add(new AFSearchToken(AFSearchFilter.Template, AFSearchOperator.Equal, HA_Template));
          using (AFElementSearch childrenSearch2 = new AFElementSearch(AFdB, "ChildrenSearch", tokens))
          {
              childrenSearch2.CacheTimeout = TimeSpan.FromMinutes(10);
              IEnumerable<AFElement> children2 = childrenSearch2.FindElements(fullLoad: false);
              foreach (AFElement child in children2)
              {
                  // do something here
              }
          } // AFSearch gets immediately disposed here.
          
          

           

          Query String

          string query = $"Parent:'{CurrentElement.GetPath(AFdB)}' Template:'{HA_Template}'";
          using (AFElementSearch childrenSearch2 = new AFElementSearch(AFdB, "ChildrenSearch", query))
          {
              childrenSearch2.CacheTimeout = TimeSpan.FromMinutes(10);
              IEnumerable<AFElement> children2 = childrenSearch2.FindElements(fullLoad: false);
              foreach (AFElement child in children2)
              {
                  // do something here
              }
          } // AFSearch gets immediately disposed here.
          

           

          The other thing that stands out is you've already found CurrentElement.  If you have also already loaded CurrentElement, it may be quicker just to search on it.

          private IList<AFElement> GetChildElements(AFElement parentElement, string childTemplateName)
          {
              IList<AFElement> list = new List<AFElement>();
              AFElementTemplate childTemplate = parentElement.Database.ElementTemplates[childTemplateName];
              foreach (AFElement childElement in parentElement.Elements)
              {
                  if (childElement.Template == childTemplate)
                  {
                      list.Add(childElement);
                  }
              }
              return list;
          }
          

           

          And you would invoke that like:

          IList<AFElement> childElements = GetChildElements(CurrentElement, HA_Template);
          foreach (AFElement child in childElements)
          {
              // do something here
          }
          
          3 of 3 people found this helpful