Apply LayerX Files from Portal

132
2
11-25-2019 03:33 AM
New Contributor III

I can store a layerX file in Portal and access from my Pro Client with it's item ID:

Item testLayerLryx = ItemFactory.Instance.Create(lyrxId, ItemFactory.ItemType.PortalItem);

Great. However - can I use this object to apply symbology from the layerX entity to an existing layer already loaded into Pro. It looks tricky as the constructor only seems to accept a filepath and a CimLayerDocument.

LayerDocument layerDoc = new LayerDocument();

- but I may going down the wrong path in any case.

Also, the LayerFactory only makes layers, but I can't see anything similar for layer files.

Anyway, any help - much appreciated.

Tags (2)
Reply
0 Kudos
2 Replies
Regular Contributor

Hi Simon,

To apply symbology from lyr/lyrx file you need to use geoprocessing:

var parameters = Geoprocessing.MakeValueArray(featLayer, lyrFilePath);

var gpResult = Geoprocessing.ExecuteToolAsync("ApplySymbologyFromLayer_management", parameters,

   null, null, null, GPExecuteToolFlags.GPThread);

Reply
0 Kudos
Esri Regular Contributor

Hi Simon,

You can create a layer from an item using this overload of the CreateLayer method

public Layer CreateLayer( 
Item item,
ILayerContainerEdit mapOrGroupLayer,
int index,
string layerName
)

You can apply your lyrX file's symbology to a layer in the active map using code similar to the snippet below:

QueuedTask.Run( () => {

Item portalLayerLryxItem = ItemFactory.Instance.Create(lyrXPortalItemID, ItemFactory.ItemType.PortalItem);
var featureLayer = LayerFactory.Instance.CreateLayer(portalLayerLryxItem, MapView.Active.Map);
var lyrDocFromLyrxFile = new LayerDocument(featureLayer);
var cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument();

//Get the renderer from the layer file
var rendererFromLayerFile = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMSimpleRenderer;
var featureLayerToApplySymbologyTo = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(f => f.Name.Contains("Cities")).FirstOrDefault(); ;

//Apply the renderer to the feature layer
featureLayerToApplySymbologyTo?.SetRenderer(rendererFromLayerFile);
});

Thanks

Uma

Reply
0 Kudos