Adding a layer to a hosted feature server

5060
6
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

6 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.

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

PhilippNagel1
New Contributor III

Hi Raul,

 

Hopefully it's OK to revive this old thread. I have run into a strange issue with this workflow a few times, when adding a layer to a hosted feature layer, exactly the same way you describe above. The layer is added successfully (response indicates success), and I can also see it in the REST directory for the service, but it is not visible at all on the item page in AGOL for the service (not on the main page, not under data, visualization, etc.). The layer is also not included when adding the service to a new webmap, or to a Pro map.

I did call the `refresh` endpoint on both the service and the layer level after the operation as well, but that did not help. 

Any ideas what might cause this, or is this something I should contact support about?

0 Kudos
RyanTucker
New Contributor III

@PhilippNagel1 I have a ticket open with ESRI on this issue.

Here's what I'm seeing:

  • From ArcGIS Pro (I'm using g 3.0.1)
  • Publish feature layer with popups enabled to ArcGIS Online
  • Add new feature to published feature layer using rest endpoint Add to Definition
  • New feature shows up in feature layer under admin rest endpoint, but not under Contents

Now try the same process again. Only this time disable popups before publishing from ArcGIS Pro.

Does the new feature show up now? 

0 Kudos
PhilippNagel1
New Contributor III

Hi Ryan!

I totally forgot I had this thread going here. I was able to find out what the cause of this is, and I do have a workaround. You are indeed on the right track with what you posted.

The issue is that some configuration like pop-ups, labels, etc are stored in AGOL not at the service level, but at the item level. If this item-level data is not configured, the item page displays info pulled from the REST endpoint regarding the layers and tables that are present.

If the item-level data IS configured, this is what will be shown. And so when you use the REST endpoint to add to the service definition, it will update the service properly, but neglects to update the data stored in the item details. So now there is a discrepancy that is not being resolved anywhere.

You can manually fix this by modifying the item data, either clearing it out completely if you don't need the configuration stored here (for example, if all your layer configs are webmap driven), or you can just add a blank layer object for your added layer to the layers array in the item data.

An easy way to modify item data is through ArcGIS Online Assistant, or you can use the REST API to modify the item data: https://developers.arcgis.com/rest/users-groups-and-items/item-data.htm

 

They did file an enhancement request for this, since you are working with support, please have them attach this to your account and ask them to escalte this as well - for some reason, they think this is Low priority currently. The enhancement is

ENH-000129299

There's also a bug for the same thing. Not sure why there's 2 filings of the same issue. The bug shows closed for some reason:

#BUG-000152038

 

Here's an example of the item data before I add layer ID 1:

PhilippNagel1_0-1667401669292.png

 

And this would be the "empty" layer object for layer 1 added to make it appear on the item page:

PhilippNagel1_1-1667401704078.png