<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: C# Code for ArcGIS Pro Add-in to retrieve text string from selected record of a layer in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1123357#M7425</link>
    <description>&lt;P&gt;Something like this might work.&lt;/P&gt;&lt;P&gt;Right click on project and Add&amp;gt;New Item then choose Pro Map Tool.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KirkKuykendall1_1-1638891317521.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28991i27F4A0E97C5CB0C2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KirkKuykendall1_1-1638891317521.png" alt="KirkKuykendall1_1-1638891317521.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the daml, change the condition:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KirkKuykendall1_0-1638891124870.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28990iAC276750BC510680/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KirkKuykendall1_0-1638891124870.png" alt="KirkKuykendall1_0-1638891124870.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Change the&amp;nbsp;OnSketchCompleteAsync override:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ToolTester
{
    internal class MapTool1 : MapTool
    {
        public MapTool1()
        {
            IsSketchTool = true;
            SketchType = SketchGeometryType.Point;
            SketchOutputMode = SketchOutputMode.Map;
        }

        protected override Task OnToolActivateAsync(bool active)
        {
            return base.OnToolActivateAsync(active);
        }

        protected override Task&amp;lt;bool&amp;gt; OnSketchCompleteAsync(Geometry geometry)
        {
            return QueuedTask.Run(() =&amp;gt;
            {
                var fLayer = this.ActiveMapView.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
                //condition="esri_mapping_featureLayerSelectedCondition" in the daml should prevent this ...
                if (fLayer == null)
                    return true;
                //use whatever field is been set as Display Field in the layer properties ...
                var fldName = ((CIMFeatureLayer)fLayer.GetDefinition()).FeatureTable.DisplayField;
                string fileName = GetFirstRow(fLayer, fldName, geometry);
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    Uri myUri = new Uri($@"C:\Temp\{fileName}.lyrx");
                    Debug.Print(fileName);
                    //todo: verify file exists, layer not already added, etc.
                    //Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(myUri, this.ActiveMapView.Map, LayerPosition.AddToTop);
                }
                return true;
            });
        }
        private string GetFirstRow(FeatureLayer fLayer, string fldName, Geometry geom)
        {
            string s = "";
            GeometryEngine.Instance.Project(geom, fLayer.GetSpatialReference());
            using (var cursor = fLayer.Search(new SpatialQueryFilter()
            {
                FilterGeometry = GeometryEngine.Instance.Project(geom, fLayer.GetSpatialReference()),
                SpatialRelationship = SpatialRelationship.Intersects,
                SubFields = fldName
            }))
            {
                if (cursor.MoveNext())
                    s = cursor.Current[fldName].ToString();
            };
            return s;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Dec 2021 15:35:30 GMT</pubDate>
    <dc:creator>KirkKuykendall1</dc:creator>
    <dc:date>2021-12-07T15:35:30Z</dc:date>
    <item>
      <title>C# Code for ArcGIS Pro Add-in to retrieve text string from selected record of a layer</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1121104#M7398</link>
      <description>&lt;P&gt;Learning C# and I am looking for some code that will allow me to create a tool for the Map View, that when positioned over an individual polygon and selected by the tool, would return the text string (e.g. CntyGeo.lyrx) from the attribute field (say "Link") of the selected feature layer in the Contents Pane.&lt;BR /&gt;I would use this returned individual text string to reference to another C# code that adds a .lyrx file to the Map View (see C# code below). The text string would be used to add an .lyrx from the C:\ drive (e.g. CntyGeo.lyrx) to the Map View which currently works:&lt;BR /&gt;protected override void OnClick()&lt;BR /&gt;{&lt;BR /&gt;try { QueuedTask.Run(() =&amp;gt; { Uri myUri = new Uri(@"C:\Temp\CntyGeo.lyrx"); Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(myUri, MapView.Active.Map, LayerPosition.AddToTop); }); }&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;MessageBox.Show($@"Error: {ex.ToString()}");&lt;BR /&gt;}&lt;BR /&gt;I'm using ArcGIS Pro 2.8 and MS Visual Studio 2019. Thanks for any help.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;</description>
      <pubDate>Tue, 30 Nov 2021 01:40:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1121104#M7398</guid>
      <dc:creator>JohnHanson1</dc:creator>
      <dc:date>2021-11-30T01:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: C# Code for ArcGIS Pro Add-in to retrieve text string from selected record of a layer</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1123357#M7425</link>
      <description>&lt;P&gt;Something like this might work.&lt;/P&gt;&lt;P&gt;Right click on project and Add&amp;gt;New Item then choose Pro Map Tool.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KirkKuykendall1_1-1638891317521.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28991i27F4A0E97C5CB0C2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KirkKuykendall1_1-1638891317521.png" alt="KirkKuykendall1_1-1638891317521.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the daml, change the condition:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KirkKuykendall1_0-1638891124870.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28990iAC276750BC510680/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KirkKuykendall1_0-1638891124870.png" alt="KirkKuykendall1_0-1638891124870.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Change the&amp;nbsp;OnSketchCompleteAsync override:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ToolTester
{
    internal class MapTool1 : MapTool
    {
        public MapTool1()
        {
            IsSketchTool = true;
            SketchType = SketchGeometryType.Point;
            SketchOutputMode = SketchOutputMode.Map;
        }

        protected override Task OnToolActivateAsync(bool active)
        {
            return base.OnToolActivateAsync(active);
        }

        protected override Task&amp;lt;bool&amp;gt; OnSketchCompleteAsync(Geometry geometry)
        {
            return QueuedTask.Run(() =&amp;gt;
            {
                var fLayer = this.ActiveMapView.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
                //condition="esri_mapping_featureLayerSelectedCondition" in the daml should prevent this ...
                if (fLayer == null)
                    return true;
                //use whatever field is been set as Display Field in the layer properties ...
                var fldName = ((CIMFeatureLayer)fLayer.GetDefinition()).FeatureTable.DisplayField;
                string fileName = GetFirstRow(fLayer, fldName, geometry);
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    Uri myUri = new Uri($@"C:\Temp\{fileName}.lyrx");
                    Debug.Print(fileName);
                    //todo: verify file exists, layer not already added, etc.
                    //Layer newlayer = LayerFactory.Instance.CreateFeatureLayer(myUri, this.ActiveMapView.Map, LayerPosition.AddToTop);
                }
                return true;
            });
        }
        private string GetFirstRow(FeatureLayer fLayer, string fldName, Geometry geom)
        {
            string s = "";
            GeometryEngine.Instance.Project(geom, fLayer.GetSpatialReference());
            using (var cursor = fLayer.Search(new SpatialQueryFilter()
            {
                FilterGeometry = GeometryEngine.Instance.Project(geom, fLayer.GetSpatialReference()),
                SpatialRelationship = SpatialRelationship.Intersects,
                SubFields = fldName
            }))
            {
                if (cursor.MoveNext())
                    s = cursor.Current[fldName].ToString();
            };
            return s;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 15:35:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1123357#M7425</guid>
      <dc:creator>KirkKuykendall1</dc:creator>
      <dc:date>2021-12-07T15:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: C# Code for ArcGIS Pro Add-in to retrieve text string from selected record of a layer</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1123916#M7446</link>
      <description>&lt;P&gt;Mr.&amp;nbsp;&lt;SPAN&gt;Kuykendall,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Perfect!&amp;nbsp; Thank you very much!&amp;nbsp; I had added some C# code to add layers from a selection, and it works like a charm.&amp;nbsp; I attached the modified code of the MapTool1.cs (zipped) below.&lt;/P&gt;&lt;P&gt;Once again, Thank you&lt;/P&gt;&lt;P&gt;John Hanson&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 17:28:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-code-for-arcgis-pro-add-in-to-retrieve-text/m-p/1123916#M7446</guid>
      <dc:creator>JohnHanson1</dc:creator>
      <dc:date>2021-12-08T17:28:42Z</dc:date>
    </item>
  </channel>
</rss>

