ArcGIS Simple Edit service
ArcGIS_SimpleEdit_CSharp\ArcGIS_SimpleEdit_WebService\App_Code\AddActionLocationService.cs
// 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.
// 

[System.Web.Services.WebService(Namespace = "http://localhost/ArcGIS_SimpleEdit_WebService/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
public class AddActionLocationService : System.Web.Services.WebService
{
    #region Instance Properties

    // Structure to store the location of an action
    public struct ActionLocation
    {
        public ActionLocation(double x, double y)
        {
            X = x;
            Y = y;
        }

        public double X;
        public double Y;
    }

    // Structure to store the properties of an action
    public struct ActionRecord
    {
        public string Name;
        public string Details;
        public ServiceType ServiceType;
        public ActionLocation Location;
    }

    // Enumeration of the possible service types
    public enum ServiceType { Start, Stop, Repair }

    #endregion

    #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 int AddActionLocation(AddActionLocationService.ActionRecord actionRecord)
    {

        ESRI.ArcGIS.Server.IServerContext serverContext = null;
        ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
        
        // Declare connection parameter variables
        string serverName = "localhost";
        string serverType = "MapServer";
        string serviceName = "MontgomerySimple";
        string layerName = "ServiceCalls";

        try
        {
                    
            // Connect to ArcGIS Server
            ESRI.ArcGIS.ADF.Identity iden = new ESRI.ArcGIS.ADF.Identity("username", "password", "domainOrmachine");
            ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection 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
            ESRI.ArcGIS.Server.IServerObjectManager serverObjectManager = agsServerConnection.ServerObjectManager;
            serverContext = serverObjectManager.CreateServerContext(serviceName, serverType);

            // Get access to the fine-grained ArcObjects underlying the map service via 
            // the server context's MapServerObjects
            ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
            ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
            
            // Get a reference to the map underlying the map service
            ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);

            // Loop through the layers in the map until the service calls feature class is found
            ESRI.ArcGIS.Carto.IEnumLayer enumLayer = aoMap.get_Layers(null, true);
            enumLayer.Reset();            
            ESRI.ArcGIS.Carto.ILayer aoLayer = enumLayer.Next();

            ESRI.ArcGIS.Carto.IFeatureLayer aoFeatureLayer = null;            
            while (aoLayer != null)
            {
                if (aoLayer.Name == layerName)
                {
                    aoFeatureLayer = aoLayer as ESRI.ArcGIS.Carto.IFeatureLayer;
                    break;
                }
                aoLayer = enumLayer.Next();
            }

            // If the layer was not found, adding the feature was unsuccessful, so return -1
            if (aoFeatureLayer == null)
                return -1;

            // Get the ArcObjects feature class and dataset underlying the service calls layer
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = aoFeatureLayer.FeatureClass;
            ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;

            // Create an ArcObjects point with the location of the passed-in record
            ESRI.ArcGIS.Geometry.IPoint aoPoint = serverContext.CreateObject("esriGeometry.Point") as 
                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 = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
            ESRI.ArcGIS.Geodatabase.IMultiuserWorkspaceEdit multiuserworkspaceedit = (ESRI.ArcGIS.Geodatabase.IMultiuserWorkspaceEdit)workspaceEdit;
            if (!(workspaceEdit.IsBeingEdited()))
            {                
                // 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
                ESRI.ArcGIS.Geodatabase.IFeature feature = featureClass.CreateFeature();
                feature.Shape = aoPoint;
                feature.set_Value(featureClass.FindField("Name"), actionRecord.Name);
                feature.set_Value(featureClass.FindField("Service_Details"), actionRecord.Details);
                feature.set_Value(featureClass.FindField("Service_Type"), actionRecord.ServiceType.ToString());
                int trackingNumber = feature.OID;
                feature.set_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;
            }

        }
        catch (System.Exception 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 (workspaceEdit != null)
            {
                if (workspaceEdit.IsBeingEdited())
                    workspaceEdit.StopEditing(false);
            }
            if (serverContext != null)
                serverContext.ReleaseContext();
        }
    }

    #endregion
}