Programmatically publishing a feature service from an existing one

854
4
12-06-2017 06:59 AM
KenGorton
Esri Contributor

How do you programmatically publishing a new feature service from an existing one?

In my app I would like to enable users with appropriate privileges to publish a new feature service by using an existing feature service url.

I use a network request which partially looks like this:

NetworkRequest {

id: createServiceNetworkRequest

method: "POST"

url: portalUrl + "/sharing/rest/content/users/" + username + "/publish"

responseType: "json"

The body of the request looks something like this:

{"access":"","description":"","f":"json","fileType":"featureService","itemid":"<<itemid of source feature service>>","name":"My New Feature Service","publishParameters":{"sourceUrl":"<<url to source feature service>>},"snippet":"A new feature service","tags":"my tag","token":"<<my valid token>>"}

But the response comes back as follows: 

"{\"error\":{\"code\":400,\"message\":\"Unable to publish item.\",\"details\":[\"'publishParameters' must be specified.\"]}}"

Clearly I am not forming the request correctly. Or perhaps there is an entirely different means to this end? How should I be doing this?

Thanks,

Ken

0 Kudos
4 Replies
ErwinSoekianto
Esri Regular Contributor

Ken, 

Can you reproduce the same issue via the REST endpoint? 

Trying the same request via PostMan. 

It would be easier if we make sure the request is successful first directly via REST then construct it in QML.

Thanks,

Erwin.

0 Kudos
KenGorton
Esri Contributor

So I was finally able to get back to this and by inspecting the requests/responses in my browser dev tools while going through this using the AGOL interface I figured out what I think is the correct approach:

  • fetch the json of the template feature service which I store in a variable templateJson
  • call isServiceNameAvailable and pass in the proposed name of the new service: the response I get is "{\"available\":true}" which I know it is.
  • call createService passing in something like this json: 

"createParameters": {

"currentVersion": templateJson.currentVersion,

"serviceItemId": templateJson.serviceItemId,

"serviceDescription": "",

"hasVersionedData": false,

"supportsDisconnectedEditing": false,

"hasStaticData": false,

"maxRecordCount": templateJson.maxRecordCount,

"supportedQueryFormats": "JSON",

"supportsVCSProjection": templateJson.supportsVCSProjection,

"capabilities": templateJson.capabilities,

"description": "",

"copyrightText": "",

"allowGeometryUpdates": templateJson.allowGeometryUpdates,

"units": templateJson.units,

"supportsAppend": templateJson.supportsAppend,

"syncEnabled": templateJson.syncEnabled,

"extractChangesCapabilities": templateJson.extractChangesCapabilities,

"supportsApplyEditsWithGlobalIds": templateJson.supportsApplyEditsWithGlobalIds,

"supportsOBACForAnonymousUsers": templateJson.supportsOBACForAnonymousUsers,

"editorTrackingInfo": templateJson.editorTrackingInfo,

"changeTrackingInfo": templateJson.changeTrackingInfo,

"xssPreventionInfo": templateJson.xssPreventionInfo,

"name" the proposed name of the new service

},

"targetType": "featureService",

"f": "json",

"token": myToken

the response I get is: "{\"success\":false,\"error\":{\"message\":\"Service name 'null' already exists for 'Nxxxxxxxxxxxxxx1'\"},\"isView\":false}"

So what value am I not sending in correctly and why does it tell me the name is available and then tell me otherwise?

Thanks,

Ken

0 Kudos
ErwinSoekianto
Esri Regular Contributor

The request looks good. It looks like it received the service name as 'null' instead, as shown in the error message. 

0 Kudos
KenGorton
Esri Contributor

Ok It turned out to be a simple fix to what is probably a noob problem. I originally built the body parameter of the NetworkRequest as json and passed it in send(body). This never gave me problems with other NetworkRequests but for each of those I construct body parameters of flat json. This one has nested json, but by calling send(JSON.stringify(body)) it completes successfully.

Ken

0 Kudos