ArcGIS Simple Edit service
ArcGIS_SimpleEdit_VBNet\ArcGIS_SimpleEdit_WebService\App_Code\AddActionLocationService.vb
' Copyright 2010 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
<System.Web.Services.WebService(Namespace := "http://localhost/ArcGIS_SimpleEdit_WebService/"), System.Web.Services.WebServiceBinding(ConformsTo := System.Web.Services.WsiProfiles.BasicProfile1_1)> _
Public Class AddActionLocationService
  Inherits System.Web.Services.WebService
  #Region "Instance Properties"

  ' Structure to store the location of an action
  Public Structure ActionLocation
    Public Sub New(ByVal x As Double, ByVal y As Double)
      Me.X = x
      Me.Y = y
    End Sub

    Public X As Double
    Public Y As Double
  End Structure

  ' Structure to store the properties of an action
  Public Structure ActionRecord
    Public Name As String
    Public Details As String
    Public ServiceType As ServiceType
    Public Location As ActionLocation
  End Structure

  ' Enumeration of the possible service types
  Public Enum ServiceType
    Start
    [Stop]
    Repair
  End Enum

  #End Region

  #Region "Instance Methods"

  ' Adds a new point with the attributes and geometry encapsulated by the passed-in ActionRecord 
  ' to the ServiceCalls layer of the ParcelsEditServiceCalls service on localhost.  Note that the
  ' layer, service, and server names are hard-coded.
  <System.Web.Services.WebMethod> _
  Public Function AddActionLocation(ByVal actionRecord As AddActionLocationService.ActionRecord) As Integer
    Dim serverContext As ESRI.ArcGIS.Server.IServerContext = Nothing
    Dim workspaceEdit As ESRI.ArcGIS.Geodatabase.IWorkspaceEdit = Nothing

    ' Declare connection parameter variables
    Dim serverName As String = "localhost"
    Dim serverType As String = "MapServer"
        Dim serviceName As String = "MontgomerySimple"
    Dim layerName As String = "ServiceCalls"

    Try
            ' Connect to ArcGIS Server
            Dim iden As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("username", "password", "machineordomain")

            Dim agsServerConnection As ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection = New ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("localhost", iden)
      agsServerConnection.Host = serverName
      agsServerConnection.Connect()

      ' Create a server context in which to perform server operations
      Dim serverObjectManager As ESRI.ArcGIS.Server.IServerObjectManager = agsServerConnection.ServerObjectManager
      serverContext = serverObjectManager.CreateServerContext(serviceName, serverType)

      ' Get access to the fine-grained ArcObjects underlying the map service via 
      ' the server context's MapServerObjects
      Dim mapServer As ESRI.ArcGIS.Carto.IMapServer = TryCast(serverContext.ServerObject, ESRI.ArcGIS.Carto.IMapServer)
      Dim mapServerObjects As ESRI.ArcGIS.Carto.IMapServerObjects = TryCast(mapServer, ESRI.ArcGIS.Carto.IMapServerObjects)

      ' Get a reference to the map underlying the map service
            Dim aoMap As ESRI.ArcGIS.Carto.IMap = mapServerObjects.Map(mapServer.DefaultMapName)

      ' Loop through the layers in the map until the service calls feature class is found
            Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = aoMap.Layers(Nothing, True)
      enumLayer.Reset()
      Dim aoLayer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next()

      Dim aoFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = Nothing
      Do While Not aoLayer Is Nothing
        If aoLayer.Name = layerName Then
          aoFeatureLayer = TryCast(aoLayer, ESRI.ArcGIS.Carto.IFeatureLayer)
          Exit Do
        End If
        aoLayer = enumLayer.Next()
      Loop

      ' If the layer was not found, adding the feature was unsuccessful, so return -1
      If aoFeatureLayer Is Nothing Then
        Return -1
      End If

      ' Get the ArcObjects feature class and dataset underlying the service calls layer
      Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = aoFeatureLayer.FeatureClass
      Dim aoDataset As ESRI.ArcGIS.Geodatabase.IDataset = TryCast(featureClass, ESRI.ArcGIS.Geodatabase.IDataset)

      ' Create an ArcObjects point with the location of the passed-in record
      Dim aoPoint As ESRI.ArcGIS.Geometry.IPoint = TryCast(serverContext.CreateObject("esriGeometry.Point"), ESRI.ArcGIS.Geometry.IPoint)
      aoPoint.PutCoords(actionRecord.Location.X, actionRecord.Location.Y)

      ' Make sure the service call feature class's workspace is not being edited before attempting
      ' to add a new feature
      workspaceEdit = TryCast(aoDataset.Workspace, ESRI.ArcGIS.Geodatabase.IWorkspaceEdit)
      If Not(workspaceEdit.IsBeingEdited()) Then
        ' Start the edit operation
                workspaceEdit.StartEditing(False)
                workspaceEdit.StartEditOperation()

        ' Create a feature and set its geometry and attributes to those specified by the
        ' passed-in action record
        Dim feature As ESRI.ArcGIS.Geodatabase.IFeature = featureClass.CreateFeature()
        feature.Shape = aoPoint
                feature.Value(featureClass.FindField("Name")) = actionRecord.Name
                feature.Value(featureClass.FindField("Service_Details")) = actionRecord.Details
                feature.Value(featureClass.FindField("Service_Type")) = actionRecord.ServiceType.ToString()
        Dim trackingNumber As Integer = feature.OID
                feature.Value(featureClass.FindField("Tracking_Number")) = trackingNumber

        ' Commit the new feature to the database
                feature.Store()
                workspaceEdit.StopEditOperation()
        workspaceEdit.StopEditing(True)

        ' Release the current server context
        serverContext.ReleaseContext()

        ' Since adding the feature was successful, return the object id of the new feature
        Return trackingNumber
      Else
        ' Adding the feature was unsuccessful, so return -1
        Return -1
      End If

    Catch exception As System.Exception
      Throw exception
    Finally
      ' Ensure that the editing operation is stopped and server context is released by
      ' putting the logic to do so in a finally block
      If Not workspaceEdit Is Nothing Then
        If workspaceEdit.IsBeingEdited() Then
          workspaceEdit.StopEditing(False)
        End If
      End If
      If Not serverContext Is Nothing Then
        serverContext.ReleaseContext()
      End If
    End Try
  End Function

  #End Region
End Class