Adding a layer to a hosted feature server

1498
3
07-01-2019 07:47 AM
AlexanderFischer
New Contributor III

Hello,

I already have a hosted feature service. Is it possible to add another layer into this? For example, when I try to publish a shapefile, the system forces me to give a different feature server name, hence it creates a separate feature server.

If this is indeed possible, how can I do it by using the REST API?

Thanks

3 Replies
KellyGerrow
Esri Frequent Contributor

Potentially you could update an existing feature layer published from a Shapefile, SD file or a FGDB and add another layer to an existing service. If a lot of web maps already reference the feature layer, I'd recommend just publishing a new layer as a new service. This way you can easily find and add the new data to webmaps without needing to worry about it updating and working with content that is already created.

0 Kudos
AlexanderFischer
New Contributor III

Hello Kelly, thanks for the reply. The option that you mentioned is for updating the data. The documentation says:

"You must use the same source file type and schema when you overwrite a hosted feature layer."

What I am looking for is just adding another layer with a completely different schema while keeping the data in existing layers as it is.

For example, please take a look at the attached screenshot. In this case, there are already four layers in my hosted feature server. I want to add a fifth layer into this. Is it possible?

Raul_Jimenez
Esri Contributor

Hi Zeno Team‌, you can do that doing something like this (for a Point Layer Type):

const YOUR_TOKEN = "<place your token here>";
const SERVICE_NAME = "<your service name>"; // Probably similar to your item name
const HOST = "<the subdomain of your service>"; // Something like: services7
const INSTANCE = "<your instance id>"; // Something like: d9R4ThD32qsG1Wu4
const LAYER_NAME = "<your layer name>";

const layerDefinition = {
  "layers": [
    {
      "currentVersion": 10.51,
      "id": 0,
      "name": LAYER_NAME,
      "type": "Feature Layer",
      "displayField": "",
      "description": "",
      "copyrightText": "",
      "defaultVisibility": true,
      "editingInfo": {
        "lastEditDate": null
      },
      "relationships": [],
      "isDataVersioned": false,
      "supportsAppend": true,
      "supportsCalculate": true,
      "supportsTruncate": true,
      "supportsAttachmentsByUploadId": true,
      "supportsAttachmentsResizing": true,
      "supportsRollbackOnFailureParameter": true,
      "supportsStatistics": true,
      "supportsAdvancedQueries": true,
      "supportsValidateSql": true,
      "supportsCoordinatesQuantization": true,
      "supportsApplyEditsWithGlobalIds": false,
      "advancedQueryCapabilities": {
        "supportsPagination": true,
        "supportsPaginationOnAggregatedQueries": true,
        "supportsQueryRelatedPagination": true,
        "supportsQueryWithDistance": true,
        "supportsReturningQueryExtent": true,
        "supportsStatistics": true,
        "supportsOrderBy": true,
        "supportsDistinct": true,
        "supportsQueryWithResultType": true,
        "supportsSqlExpression": true,
        "supportsAdvancedQueryRelated": true,
        "supportsCountDistinct": true,
        "supportsLod": true,
        "supportsReturningGeometryCentroid": false,
        "supportsQueryWithDatumTransformation": true,
        "supportsHavingClause": true,
        "supportsOutFieldSQLExpression": true
      },
      "useStandardizedQueries": true,
      "geometryType": "esriGeometryPoint",
      "minScale": 0,
      "maxScale": 0,
      "extent": {
        "xmin": -0.000004988163709640503,
        "ymin": 2504688.542852979,
        "xmax": 2504688.542843003,
        "ymax": 5009377.085700966,
        "spatialReference": {
          "wkid": 102100
        }
      },
      "allowGeometryUpdates": true,
      "hasAttachments": false,
      "htmlPopupType": "esriServerHTMLPopupTypeNone",
      "hasM": false,
      "hasZ": false,
      "objectIdField": "OBJECTID",
      "uniqueIdField": {
        "name": "OBJECTID",
        "isSystemMaintained": true
      },
      "globalIdField": "",
      "typeIdField": "",
      "fields": [
        {
          "name": "OBJECTID",
          "type": "esriFieldTypeOID",
          "alias": "OBJECTID",
          "sqlType": "sqlTypeOther",
          "nullable": false,
          "editable": false,
          "domain": null,
          "defaultValue": null
        },
        {
          "name": "Name",
          "type": "esriFieldTypeString",
          "actualType": "nvarchar",
          "alias": "Name",
          "sqlType": "sqlTypeNVarchar",
          "length": 256,
          "nullable": true,
          "editable": true,
          "visible": true,
          "domain": null,
          "defaultValue": null
        }
      ],
      "indexes": [],
      "types": [],
      "supportedQueryFormats": "JSON, geoJSON",
      "hasStaticData": false,
      "maxRecordCount": 2000,
      "standardMaxRecordCount": 32000,
      "tileMaxRecordCount": 8000,
      "maxRecordCountFactor": 1,
      "capabilities": "Query,Editing,Create,Update,Delete,Sync",
      "syncEnabled": true,
      "adminLayerInfo": {
        "geometryField": {
          "name": "Shape",
          "srid": 102100
        }
      }
    }
  ],
  "tables": []
}

const settings = {
  "url": `https://${HOST}.arcgis.com/${INSTANCE}/arcgis/rest/admin/services/${SERVICE_NAME}/FeatureServer/addToDefinition`,
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  "data": {
    "addToDefinition": JSON.stringify(layerDefinition),
    "f": "json",
    "token": YOUR_TOKEN
  }
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

In November I wrote some documentation about how to work with hosted feature services through the REST API.

Best regards,

Raul