[GIS] ArcGIS Javascript API Query & Add features without map using REST API

arcgis-javascript-apiarcgis-server

I need to query arcGIS map service and add this result to another feature service using ArcGIS JavaScript API. I should not use MAP object.

I did query data without map from ArcGIS sample: Now i need to post this data to feature service:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Query State Info without Map</title>

    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
        require([
          "esri/tasks/query", "esri/tasks/QueryTask",
          "dojo/dom", "dojo/on", "dojo/domReady!"
        ], function (Query, QueryTask, dom, on) {

            var queryTask = new QueryTask("http://xxxx/arcgis/rest/services/REGIS/xxx/MapServer/1");

            var query = new Query();
            query.returnGeometry = true;
            query.outFields = [
              "PIN", "PDAREA "
            ];

            on(this, "load", execute);

            function execute() {
                //query.where = "PIN in (" + getParameterByName("PINS") + ")";
                query.where = "PIN in (1010027)";
                queryTask.execute(query, showResults);
            }

            function showResults(results) {
                var resultItems = [];
                var resultCount = results.features.length;
                for (var i = 0; i < resultCount; i++) {
                    newfeatGeom = JSON.stringify(results.features[i].geometry);
                    newAtt = JSON.stringify(results.features[i].attributes);
                    finalObj = newfeatGeom.concat(newAtt);
                    var featureAttributes = results.features[i].attributes;
                    for (var attr in featureAttributes) {
                        resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
                    }
                    resultItems.push("<br>");
                }
                dom.byId("info").innerHTML = resultItems.join("");
            }

            function getParameterByName(name) {
                name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
                var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                    results = regex.exec(location.search);
                return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
            }
        });
    </script>
</head>

<body>
    US state name :
    <br />
    <div id="info" style="padding: 5px; margin: 5px; background-color: #eee;">
    </div>
</body>
</html>

Any Idea?

Best Answer

Finally I have the answer for my Query: Here is the solution... (I am posting this; it might help someone) Thank You for your Help and Support.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Copy data from one service to another using REST API</title>

    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
        var objPD = [{ "attributes": { "OBJECTID": 1, "SAM_NO": 0, "PIN": "1010027", "C_SRC": " ", "TRANS_NO": 100, "D_IN_DATE": null, "I_WORKORDER_ID": null, "TAIN_NO": null, "TAIN_YEAR": null, "PAR_AREA": 174804 }, "geometry": null }];
        require([
          "esri/tasks/query", "esri/tasks/QueryTask",
          "dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json",
          "esri/urlUtils", "esri/config", "esri/request", "dojo/domReady!"],
          function (Query, QueryTask, dom, on, domClass, dojoJson, urlUtils, esriConfig, esriRequest) {
              esriConfig.defaults.io.proxyUrl = "http://localhost:52896/proxy.ashx";
              var queryTask = new QueryTask("http://xxx/arcgis/rest/services/xxx/xxx/MapServer/1");
              var query = new Query();
              query.returnGeometry = true;
              query.outFields = [
                "PIN", "PDAREA "
              ];
              dom.byId("info").innerHTML = "Processing...";
              on(this, "load", execute);

              function execute() {
                  query.where = "PIN in (1010027)";
                  queryTask.execute(query, showResults);
              }

              function showResults(results) {
                  var resultItems = [];
                  var resultCount = results.features.length;
                  var today = new Date();                 
                  for (var i = 0; i < resultCount; i++) {
                      objPD[0].geometry = results.features[i].geometry;
                      objPD[0].attributes.PIN = results.features[i].attributes.PIN;
                      objPD[0].attributes.PAR_AREA = results.features[i].attributes.PDAREA;
                      newAtt = JSON.stringify(objPD);
                      dom.byId("info").innerHTML = "Copying PIN: " + objPD[0].attributes.PIN;
                      var requestHandle = esriRequest({
                           "url": "http://xxxx/arcgis/rest/services/xxxx/xxxx/FeatureServer/2/addFeatures",                         
                          "content": {
                              "features": newAtt,
                              "f": "json"}}, {"usePost": true});
                      requestHandle.then(requestSucceeded, requestFailed);
                  }

              }

              function getParameterByName(name) {
                  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
                  var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                      results = regex.exec(location.search);
                  return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
              }
              function requestSucceeded(response, io) {
                  dom.byId("info").innerHTML = dojoJson.toJson(response, true);
              }
              function requestFailed(error, io) {
                  dom.byId("info").innerHTML = dojoJson.toJson(error, true);

              }
          });
    </script>
</head>

<body>

    <div id="info" style="padding: 5px; margin: 5px; background-color: #eee;">
    </div>
</body>
</html>
Related Question