Problems with creating a Feature Service with Rest JS and adding it to a Map

629
1
03-11-2020 10:30 AM
JonM_
by
New Contributor II

I am creating a browser app that integrates the ArcGIS API for Javascript, and ArcGIS Rest JS in order to allow users to create and edit Feature Layers hosted on ArcGIS Online. I am able to create a hosted Feature Layer that appears in my 'Contents' on ArcGIS Online, but I am unable to add this layer to my WebMap. Here is the relevant function below, called by a widget when clicked:

function createSite() {
    // pass credentials of auth session from JS API to Rest JS
    esriId.getCredential("https://arcgis.com/sharing/rest/")
    .then(cred => {
        const session = new arcgisRest.UserSession.fromCredential(cred);

        // create the item
        arcgisRest.createFeatureService({
        item: {
            name: sitename,
            capabilities: "Create, Delete, Query, Update",
            }, 
        authentication: session
        })
            //get url of newly created feature service
            .then(response =>{
                var newURL = response.encodedServiceURL;

                // add service definition
                arcgisRest.addToServiceDefinition(newURL, {
                    authentication: session,
                    layers: [ {name: "thisLayer",
                               type: "Feature Layer",
                               geometryType: "esriGeometryPoint",
                               fields: [
                                   {
                                       name: "FID",
                                       type: "esriFieldTypeInteger",
                                       actualType: "int"
                                   }
                                ]
                    } ]
                });
            });
    });

    // refresh layers on map view - remove all, query, and load again
    map.layers.removeAll();
    loadLayers();
};‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Again, this successfully creates a Feature Layer in my ArcGIS Online Contents. However, when loadLayers() is called - which successfully loads the other Feature Layers from my account into the WebMap - the new layer does not appear. Likewise, when I try to open this new layer directly in the 'Map Viewer' on ArcGIS Online, nothing happens and the layer does not appear in the contents pane of the map.

Is this a problem with the layer details of the new Feature Layer? In other words, are there parameters and/or fields that I need to include in the addToServiceDefinition call in order for the newly created Feature Layer to have a valid format? Or is it possible to specify at creation that this new Feature Layer should mimic a template, such as the 'Points' template that is available when creating Feature Layers manually on ArcGIS Online?

In short, I need the new Feature Layer I create to be valid and able to display and edit in the WebMap viewer. Any help with this would be greatly appreciated. Thanks.

0 Kudos
1 Reply
GregoryLewis2
New Contributor

Did you get to the bottom of this? Your sample code got me over a hurdle I was having.

0 Kudos