Perform Edits using the Feature Service

What is a Feature Service?

A Feature Service is a web-based service that allows users to access, query, and edit geographic features and their attributes. Feature services provide clients with the ability to perform edits for an enhanced editing experience, including editing data from relationship classes and nonspatial tables.

Why use the Feature Service?

The Feature Service class allows users to edit multiple layers with only one applyEdits() operation. The FeatureLayer class only allows users to edit one layer per applyEdits() operation.

Loading the Feature Service Class

The Feature Service class is constructed using the url of a feature service. After the class has been instantiated, the user should load the service. For example:

Use dark colors for code blocksCopy
1
2
3
4
const featureService = new FeatureService({
  url: "https://test.com/server/rest/services/testService/FeatureServer"
});
await featureService.load();

Define the edit parameters

There are many options when setting up the edit parameters. The ServiceEditOptions argument expects an id which represents the layer id of a layer on a Feature Service, and identifierFields which is the objectIdField and globalIdField. The Feature Service needs to be defined for the applyEdits method to perform the proper edits. The applyEdits method expects the edits to be configured in a specific way. The edit below is configured to add a Polygon feature on the specified layerId.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const layer = new FeatureLayer({
  url: "https://test.com/server/rest/services/testService/FeatureServer/5"
});
layer.load();
const edits =
 {
    id: layer.layerId,
    identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
    addFeatures: [
     new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
         new Graphic({
            geometry: new Polygon({
                      hasZ: true,
                      hasM: true,
                      rings: [
                        [
                          // first ring
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4] // same as first vertex
                        ],
                        [
                          // second ring
                          [-97, 32, 35],
                          [-97, 32, 35],
                          [-97, 32, 35],
                          [-97, 32, 35] // same as first vertex
                        ]
                      ],
                      spatialReference: {
                        wkid: 26911,
                        latestWkid: 26911,
                        vcsWkid: 115702,
                        latestVcsWkid: 115702
                      }
                    }),
                    attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                    visible: true
                  })
                ]
  }

The edits object should be comprised of an id, identifierFields, and ServiceEditOptions. Once the edit is configured, then the ServiceEditOptions must be configured.

applyEdits Method

The applyEdits method takes in an array of ServiceEdits and ServiceEditOptions. With the applyEdits method the user is able to edit multiple layers of a Feature Service with one REST call. A simple apply edits call is as follows:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
     const layer = new FeatureLayer({
            url: "https://test.com/server/rest/services/testService/FeatureServer/5"
          });
     await layer.load();
     await featureService.applyEdits(
            [
              {
                id: layer.layerId,
                identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
                addFeatures: [
                  new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
                  new Graphic({
                    geometry: new Polygon({
                      hasZ: true,
                      hasM: true,
                      rings: [
                        [
                          // first ring
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4],
                          [-97, 32, 35, 4] // same as first vertex
                        ],
                        [
                          // second ring
                          [-97, 32, 35],
                          [-97, 32, 35],
                          [-97, 32, 35],
                          [-97, 32, 35] // same as first vertex
                        ]
                      ],
                      spatialReference: {
                        wkid: 26911,
                        latestWkid: 26911,
                        vcsWkid: 115702,
                        latestVcsWkid: 115702
                      }
                    }),
                    attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                    visible: true
                  })
                ]
              }
            ],
            {
              gdbVersion: layer.gdbVersion,
              globalIdUsed: false,
              honorSequenceOfEdits: false,
              usePreviousEditMoment: false,
              returnServiceEditsInSourceSR: false
            }
          );

The user can also perform more complex edits. In this example, the user is performing all the operations including addFeatures, updateFeatures, deleteFeatures, addAttachments, updateAttachments, and deleteAttachments:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const mockAttachment: Attachment = {
  globalId: "123456789",
  name: "example.pdf",
  contentType: "application/pdf",
  data: "testData"
};
const mockFeatureIdentifier: FeatureIdentifier = {
  objectId: 2,
  globalId: "testGlobalId"
};
const attachmentEdit: AttachmentEdit = {
  feature: mockFeatureIdentifier,
  attachment: mockAttachment
};
await featureServiceGlobal.applyEdits(
  [
    {
      id: layer.layerId,
      identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
      addFeatures: [
        new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
        new Graphic({ attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
      ],
      updateFeatures: [
        new Graphic({
          attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
          geometry: new Polyline({
            hasZ: true,
            hasM: true,
            paths: [
              [477710.14010000043, 3630581.9410999995, 0, null],
              [477899.40060000028, 3630521.2847000007, 0, null]
            ],
            spatialReference: {
              wkid: 26911,
              latestWkid: 26911,
              vcsWkid: 115702,
              latestVcsWkid: 115702
            }
          })
        }),
        new Graphic({
          attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
          geometry: new Polyline({
            hasZ: true,
            hasM: true,
            paths: [
              [477710.14010000043, 3630581.9410999995, 0, null],
              [477899.40060000028, 3630521.2847000007, 0, null]
            ],
            spatialReference: {
              wkid: 26911,
              latestWkid: 26911,
              vcsWkid: 115702,
              latestVcsWkid: 115702
            }
          })
        })
      ],
      deleteFeatures: [
        new Graphic({ attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
      ],
      addAttachments: [attachmentEdit, attachmentEdit],
      updateAttachments: [attachmentEdit, attachmentEdit],
      deleteAttachments: ["myAttachment", "myOtherAttachment"]
    }
  ],
  {
    gdbVersion: layer.gdbVersion,
    globalIdUsed: true,
    honorSequenceOfEdits: true,
    usePreviousEditMoment: true,
    returnServiceEditsInSourceSR: true
  }
);

Editing multiple layers at once is also possible.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
await featureServiceGlobal.applyEdits(
        [
          {
            id: 0,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
              new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
          },
          {
            id: 1,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
              new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
          },
          {
            id: 2,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
              new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
          },
          {
            id: 3,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
              new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
          }
        ],
        {
          gdbVersion: layer.gdbVersion,
          globalIdUsed: false,
          honorSequenceOfEdits: false,
          usePreviousEditMoment: false,
          returnServiceEditsInSourceSR: false
        }
      );

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.