<?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: Add Features through Enterprise REST API - Example C# .NET in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/add-features-through-enterprise-rest-api-example-c/m-p/1497266#M4775</link>
    <description>&lt;P&gt;I wanted to thank you for posting this particular sample. It saved me a bunch of time!&lt;/P&gt;</description>
    <pubDate>Tue, 25 Jun 2024 18:26:35 GMT</pubDate>
    <dc:creator>dj</dc:creator>
    <dc:date>2024-06-25T18:26:35Z</dc:date>
    <item>
      <title>Add Features through Enterprise REST API - Example C# .NET</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/add-features-through-enterprise-rest-api-example-c/m-p/1403102#M4714</link>
      <description>&lt;P&gt;I was looking through the documentation and I was having trouble finding a good example of how to add features through the API to my Workforce assignments. I figured I would put up a post up on how I was able to do get it to work.&lt;/P&gt;&lt;P&gt;Note: I use googles API to return Lat/Long from Addresses. This comes back in WKID 4326 and the hosted service is in&amp;nbsp;Spatial Reference: 102100 or WKID 3857 so I had to convert these coordinates to import.&lt;/P&gt;&lt;P&gt;I found an error in the documentation when I was looking through this doc&amp;nbsp;@&amp;nbsp;&lt;A href="https://doc.arcgis.com/en/workforce/android-phone/help/workforce-schema.htm" target="_blank" rel="noopener"&gt;https://doc.arcgis.com/en/workforce/android-phone/help/workforce-schema.htm&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;SPAN class=""&gt;SHAPE&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;The point location of the assignment.&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;An x,y coordinate pair.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is incorrect, you need to have a geometry field instead of SHAPE.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Libraries used&lt;/P&gt;&lt;P&gt;Esri.ArcGISRuntime v200.3.0&lt;/P&gt;&lt;P&gt;Newtonsoft.Json v13.0.3&lt;/P&gt;&lt;P&gt;RestSharp v110.2.0&lt;/P&gt;&lt;P&gt;Code to Convert 4326 to 3857&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        //Convert EPSG:4326 WGS 84 to EPSG:3857 WGS 84 / Pseudo-Mercator
        private double[] ConvertTo3857(double[] coords)
        {
            double smRadius = 6378136.98;
            double smRange = smRadius * Math.PI * 2.0;
            double smLonToX = smRange / 360.0;
            double smRadiansOverDegrees = Math.PI / 180.0;

            double[] newCoords = new double[2];

            newCoords[0] = coords[0];
            newCoords[1] = coords[1];

            newCoords[0] *= smLonToX;

            double y = newCoords[1];

            if (y &amp;gt; 86.0)
            {
                newCoords[1] = smRange;
            }
            else if (y &amp;lt; -86.0)
            {
                newCoords[1] = -smRange;
            }
            else
            {
                y *= smRadiansOverDegrees;
                y = Math.Log(Math.Tan(y) + (1.0 / Math.Cos(y)), Math.E);
                newCoords[1] = y * smRadius;
            }

            return newCoords;
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the class to great the object that is then converted to JSON with Newtonsoft.Json. This will have to be bracketed with [ ] once converted to string to import.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public class Assignments
    {
        public Attributes attributes { get; set; }
        public AttributeGeometry geometry { get; set; }
    }
    public class Attributes
    {
        public string description { get; set; }
        public int priority { get; set; }
        public string assignmenttype { get; set; }
        public string workorderid { get; set; }
        public long duedate { get; set; }
        public string location { get; set; }
        public string dispatcherid { get; set; }
        public int status { get; set; }

    }
    public class AttributeGeometry
    {
        public double x { get; set; }
        public double y { get; set; }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This class holds the result from importing and will give you the objectID if you need it for further in your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public class AddFeaturesResult
    {
        public Addresult[] addResults { get; set; }
    }

    public class Addresult
    {
        public bool success { get; set; }
        public string globalId { get; set; }
        public int objectId { get; set; }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is calls the portal. I pass in the Service name and the completed JSON String that will be inserted. responseObject has the response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public async Task UpdateCSUAssignments(string service, string assignment)
{
    string ServiceUrl = @"https://enterpriseUrl/server/rest/services/Hosted/" + service + @"/FeatureServer/0/addFeatures"; //Layer 0

  // generate an ArcGISTokenCredential using input from the user (username and password)
           var cred = await Esri.ArcGISRuntime.Security.AuthenticationManager.Current.GenerateCredentialAsync(
                                                           new Uri("https://enterpriseUrl/portal/sharing/rest/generateToken"),
                                                           "name",
                                                           "password") as ArcGISTokenCredential;

           //Build POST request
           var client = new RestClient(ServiceUrl);
           var request = new RestRequest(ServiceUrl, Method.Post);

           //Add Parameters
           request.AddParameter("token",cred.Token);
           request.AddParameter("f", "json");
           request.AddParameter("features", assignment);
           request.AddParameter("rollbackOnFailure", "false");

           //Execute Request
           var response = client.Execute(request);

           var responseObject = JsonConvert.DeserializeObject&amp;lt;AddFeaturesResult&amp;gt;(response.Content);

           return;
       }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;//Create Assignment object to hold everything
Assignments assignments = new Assignments();

//Create Attributes object
Attributes attributes = new Attributes(); 
attributes.status = 0;
attributes.assignmenttype = "{34AA8F52-B5A7-4DF9-9F77-169283027D14}";
attributes.location = "Full Address to import";
attributes.dispatcherid = "{AD2B0E54-FA3A-47AE-B639-95A766752E1D}";
attributes.description = "This is a test";
attributes.priority = 1;
attributes.workorderid = "ID that you want tied to Assignment";
attributes.duedate = 1711980000000; //Date that has to be in UTC format

//Create Geometry object in WKID 3857
AttributeGeometry geometry = new AttributeGeometry();
geometry.x = -10072068.9623985;
geometry.y = 4640672.87246465;

//assign attribute object to the assignment object
assignments.attributes = attributes;

//assign geometry to assignment object
assignments.geometry = geometry;

//convert object to JSON string
var json = JsonConvert.SerializeObject(assignments);

//add brackets for importation
json = "[" + json + "]";

//PortalCode is the class that contains all my API calls.
PortalCode portal = new PortalCode();

//call update assignments method
var t = System.Threading.Tasks.Task.Run(async () =&amp;gt; { await portal.UpdateCSUAssignments("This is your hosted service ID.", json); });
t.Wait();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this helps, I am sure if you change your class objects for other feature services you can use the same code to add features to your services!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Mar 2024 03:15:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/add-features-through-enterprise-rest-api-example-c/m-p/1403102#M4714</guid>
      <dc:creator>CraigSchellenbach</dc:creator>
      <dc:date>2024-03-30T03:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add Features through Enterprise REST API - Example C# .NET</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/add-features-through-enterprise-rest-api-example-c/m-p/1497266#M4775</link>
      <description>&lt;P&gt;I wanted to thank you for posting this particular sample. It saved me a bunch of time!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 18:26:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/add-features-through-enterprise-rest-api-example-c/m-p/1497266#M4775</guid>
      <dc:creator>dj</dc:creator>
      <dc:date>2024-06-25T18:26:35Z</dc:date>
    </item>
  </channel>
</rss>

