How to add layer to map from GetItemCompleted

1169
7
05-21-2013 02:11 PM
AaronConnolly
Occasional Contributor
If I do a search for layers ("type: layer") using the ArcGISPortal object:


var queryString = string.Format("{0} type:\"layer\" NOT \"web mapping application\"", searchString);

var searchParameters = new SearchParameters()
{
   QueryString = queryString,
   SortField = "avgrating",
   SortOrder = QuerySortOrder.Descending,
   Limit = 20
};

_ArcGISPortal.SearchItemsAsync(searchParameters, (results, exception) => { /* do something */ });



I get a list of ArcGISPortalItem objects. In this case they're layers, not web maps. Is it possible to add an item from these search results to an existing map object? I can't seem to find the documentation or samples that show you how to leverage "layer" items rather than "web map" items. Also, the Silverlight docs make mention of using only Web Maps. Perhaps that is a sign that you can't do anything with layers at this time.
0 Kudos
7 Replies
AaronConnolly
Occasional Contributor
Does anyone have any input on this? Is this even possible with the Silverlight SDK? Will I have to roll my own ArcGIS Online REST API Client?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I don't know any sample doing exactly that.
However a good starting point is this CreateWebMapFromObjects sample that leverage the WebMap API to create a new webmap.
0 Kudos
AaronConnolly
Occasional Contributor
That's really interesting, I didn't know you could do that. What I want to do is take the ArcGISPortalItem and create an actual layer from it. Is there a way to do this without using the WebMap API?

I've tried adding a FeatureLayer from a feature service ArcGISPortalItem object like so:


// Fetch portal items via search. Grab one portalItem ...

FeatureLayer featureLayer = new FeatureLayer();
featureLayer.Url = portalItem.Url;
featureLayer.Initialized += featureLayer_Initialized;
featureLayer.InitializationFailed += featureLayer_InitializationFailed;
featureLayer.Initialize();

MyMap.Layers.Add(featureLayer);
[\CODE]

But nothing happens. The map is not updated with the FeatureLayer.

Is there a way to map ArcGISPortalItems to the ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer, etc.... ? It looks like you should be able to do something like this, right?

Thanks,
- Aaron
0 Kudos
AaronConnolly
Occasional Contributor
This seems to work. Is this what you were thinking of?

Unfortunately, I'm passing in what I believe is a valid portal item (as it came back from the Portal API search), but the exception says "invalid item"


                // Create webmap from portal item
                WebMap.FromPortalItemAsync(portalItem, (webMap, exception) => {

                    if (exception == null) // <- Exception reads "Invalid Item"
                    {
                        Document webMapDocument = new Document();
                        webMapDocument.GetMapCompleted += (obj, args) =>
                        {
                            if (args.Map != null)
                            {
                                LayerCollection newLayers = new LayerCollection();

                                foreach (var layer in args.Map.Layers)
                                {
                                    newLayers.Add(layer);
                                }

                                args.Map.Layers.Clear();

                                MyMap.Layers = newLayers;
                            }
                        };

                        webMapDocument.GetMapAsync(webMap);
                    }
                    else
                    {
                        tbSearchItemResults.Text = exception.Message;
                    }
                });



What should I look out for when a PortalItem is declared "Invalid" by FromPortalItemAsync ?

Thanks,
- Aaron
0 Kudos
DominiqueBroux
Esri Frequent Contributor
That's really interesting, I didn't know you could do that. What I want to do is take the ArcGISPortalItem and create an actual layer from it. Is there a way to do this without using the WebMap API?

I've tried adding a FeatureLayer from a feature service ArcGISPortalItem object like so:

// Fetch portal items via search. Grab one portalItem ... 

FeatureLayer featureLayer = new FeatureLayer();
featureLayer.Url = portalItem.Url;
featureLayer.Initialized += featureLayer_Initialized;
featureLayer.InitializationFailed += featureLayer_InitializationFailed;
featureLayer.Initialize();

MyMap.Layers.Add(featureLayer);
[\CODE]

But nothing happens. The map is not updated with the FeatureLayer. 

Is there a way to map ArcGISPortalItems to the ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer, etc.... ? It looks like you should be able to do something like this, right?

Thanks,
- Aaron


If the URL of your portal item is a feature layer URL (i.e with a layerId at the end such as ....../MapServer/4), your code should work.
The risk is that your URL is the URL of a map service or a tiled service. Is this case you have to create the right layer type (i.e ArcGISMapServiceLayer or ArcGISTiledMapService layer). 
Note: It's one of the advantages of  using the webmap model. Under the cover, the service metadata are analyzed and the right layer type is created.
0 Kudos
AaronConnolly
Occasional Contributor
If the URL of your portal item is a feature layer URL (i.e with a layerId at the end such as ....../MapServer/4), your code should work.
The risk is that your URL is the URL of a map service or a tiled service. Is this case you have to create the right layer type (i.e ArcGISMapServiceLayer or ArcGISTiledMapService layer). 
Note: It's one of the advantages of  using the webmap model. Under the cover, the service metadata are analyzed and the right layer type is created.


OK - I think I get the concept now. Based on your answer to another question I asked it looks like this is the basic process to do what I want to do:

1. Search for layer type portal items.
2. Take portal item of choice and plug it into a web map that you've created programmatically
3. Take that web map and use Document to get the map from it.

Does that sound about right?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
OK - I think I get the concept now. Based on your answer to another question I asked it looks like this is the basic process to do what I want to do:

1. Search for layer type portal items.
2. Take portal item of choice and plug it into a web map that you've created programmatically
3. Take that web map and use Document to get the map from it.

Does that sound about right?

Exactly :cool:
0 Kudos