4 Replies Latest reply on Apr 9, 2018 3:58 PM by Guilherme Ferreira Branched from an earlier discussion.

    Help with element path in AFElementSearch

    Guilherme Ferreira

      I tried using AFElementSearch with an element path, but I got this "The search criteria 'Element:<elementPath>' is not valid for Element search".

       

      So I can't use element path with AFElementSearch?

       

      Regards

       

      Note to Guilherme: you tried piggybacking your question on a thread that was answered 2 years ago.  Please open new threads for new questions so that they will be flagged as needing attention.

       

      Message was edited by: Rick Davin

        • Re: Help with element path in AFElementSearch
          Rick Davin

          Hi Guilherme,

           

          What version of AF SDK are you using for Client and Server?

           

          Is your element path literally "<elementPath>" or did you edit that for obfuscation here?  If it is the literal path, then the < is being interpreted as a less than or equal operator.  Can you share with us the exact piece of code, and if variables are used in a query string or token, can you share them as well?

            • Re: Help with element path in AFElementSearch
              Guilherme Ferreira

              Hi Rick!

               

              Thank you for your response.

              <elementPath> is not my literall path. I have edited for obfuscation and reading simplicity.

               

              Using the alternate code works:

              List<string> searchPaths = new List<string>() { V_ElementPath };
              AFKeyedResults<string, AFElement> result = AFElement.FindElementsByPath(searchPaths, null);
              

               

              This doesn't:

              string token = "ELement: " + @V_ElementPath;
              AFElementSearch v_search = new AFElementSearch(v_aFDatabase, "elementSearch", token);
              AFElement v_target = v_search.FindElements() as AFElement;
              

               

              V_ElementPath contains the correct path enclosed by simple quotes.

               

              AFSDK version is 2.9.1.8106 in both AF Server and development machine (ultimately, my application will run in AF Server anyway).

               

              Regards

                • Re: Help with element path in AFElementSearch
                  Rick Davin

                  Thanks for code snippet. Backing up a bit, the Element filter is not available on AFElementSearch.  See the Filters section in Search Overview. What is available is the Name filter, where you may specify a wildcard character.

                   

                  There should be no blanks between the filter operator and the filter value.  Furthermore, you should enclose the path in quotes within the token string.

                   

                  Using String.Format:

                  string token = string.Format("Name:'{0}'", V_ElementPath);
                  

                   

                  Using Interpolated String:

                  string token = $"Name:'{V_ElementPath}'");
                  

                   

                  However, your 3rd line will not work

                  AFElement v_target = v_search.FindElements() as AFElement;
                  

                   

                  Because FindElements does not return an AFElement.  Rather it returns IEnumerable<AFElement>.  You should change it to:

                  IEnumerable<AFElement> v_target = v_search.FindElements();
                  

                   

                  Taking assessment yet again of what you what to achieve, the above potentially can return 0, 1, or many elements.  If your desire it to input one known element path in order to return exactly one element, then you should not be using AFElementSearch.  Instead you should just find the element directly within the database:

                   

                  AFElement v_target = v_afDatabase.Elements[V_ElementPath];
                  

                   

                  The above could return a null or one AFElement.  This assumes that V_ElementPath is correct, so you may need to preface with an @ sign in your earlier examples.

                  1 of 1 people found this helpful