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;
}
You could search by Item ID to get one result back.
id:31922281fed54135bf7c2b34a6e9a5e9
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?
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.
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.
This is a bit rubbish - I can't believe this is the only solution.
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.