Searching for one item exactly from Portal

558
6
04-21-2020 09:59 AM
Vidar
by
Occasional Contributor II

If I need one thing from Portal is there a way to search for it explicitly - at the moment the search result brings back a multitude of things that sound like the item I am searching for (happens to be a layer file) and also includes the actual thing I am looking for.

Ideally I want either 0 or 1 item returns so I know either that search result returned successfully or not.

This is the code I'm using where I am hoping that the "search" should work in the way that I explained above: 

public static async Task<List<AgolLayer>> GetAgolLayersAsync(int numberOfResults, string search = "")
        {
            var portal = ArcGISPortalManager.Current.GetActivePortal();
            List<AgolLayer> agolLayers = new List<AgolLayer>();

            if (portal.IsSignedOn())
            {
                var query = PortalQueryParameters.CreateForItemsOfTypes(new List<PortalItemType>() { PortalItemType.Layer }, "", Module1.GroupID, search);
                query.Limit = numberOfResults;

                //Loop until done
                var portalItems = new List<PortalItem>();
                while (query != null)
                {
                    //run the search
                    PortalQueryResultSet<PortalItem> results = await portal.SearchForContentAsync(query);
                    portalItems.AddRange(results.Results);
                    query = results.NextQueryParameters;
                }

                foreach (dynamic item in portalItems)
                {
                    AgolLayer agolLayer = new AgolLayer(item);
                    if (agolLayer.IsValidLayer)
                    {
                        agolLayers.Add(agolLayer);
                    }
                }
            }
            else
            {
                Debug.WriteLine("User not signed in - so layers cannot be retrieved.");
            }
            return agolLayers;
        }
0 Kudos
6 Replies
MarkBockenhauer
Esri Regular Contributor

You could search by Item ID to get one result back.

id:31922281fed54135bf7c2b34a6e9a5e9

0 Kudos
Vidar
by
Occasional Contributor II

Well you could - but how do you know the item id up front? Don't you have to find the item first then store away the ID somehow? Then use that in the future searches. If you do that but update the layer file at some point the ID will change as well. So that will cause a problem.

Is this the only way?

0 Kudos
MarkBockenhauer
Esri Regular Contributor

If you overwrite or update the Item the ID will not change.     I was think that you knew the specific item that you wanted to get, that is why is suggested search by ID.

0 Kudos
VictorTey
Esri Contributor

Exactly the same issue using the python api, the typical solution will be to iterate the results and find one that matches name/title/type exactly. Again if 2 items have the exact same name/title/type they will still both be returned.

0 Kudos
Vidar
by
Occasional Contributor II

This is a bit rubbish - I can't believe this is the only solution.

0 Kudos
Vidar
by
Occasional Contributor II

I have found that if I search for a layer file and include the .lyrx suffix to the search string parameter it brings back 1 result. Which is fine - but I have put a constraint to bring back "Layer Files" only. So this kind of makes the constraint for "Layer Files" superfluous. 

I'm not happy with the search algorithm here.

0 Kudos