Programatically copy certain features to new database - .NET

Discussion created by cdixon on Feb 13, 2012
Latest reply on Feb 16, 2012 by cdixon
I'm so new at ArcObjects it's a little sad, please point me in the right direction if this has been answered before or if I'm in the wrong place.

We have a customer who wants to use an ArcMap tool to expose certain parts of their map service outside the firewall so mobile users can hit it.  They want to pick a work order polygon and only include certain features within that polygon.  They would then create a new map service containing these features and expose that map service.

My first (and only, so far) approach was to have the user select the polygon(s) he would like to use, and then select one or more features from a list of features to include in the map service if they were in the polygon.  The user then previews the map, at which point I use map.ClipGeometry to cut off everything that's not in the polygons, and then set the other features the user didn't select to invisible, so the map looks like what they want.  This works well enough.

After this, the user clicks a Publish button, and I thought what that button would do is copy the map as-is to a new map service.  What's happening though is we're getting a new map service, and we're getting all of the feature definitions, but none of the data is in there.  It's just a blank map.

        private void publishBtn_Click(object sender, EventArgs e)
            IServerObjectConfiguration2 serverObjectConfiguration = null;
            bool bFound = false;

            //If the selected text is set, then we're trying to overwrite an existing one.  Prompt the user if this is ok.

            //Save the MXD
            SaveFileDialog saveFileDlg = new SaveFileDialog();
            saveFileDlg.Filter = "ArcMap Document|*.mxd";
            saveFileDlg.FileName = mapServerCbx.Text;
            saveFileDlg.DefaultExt = "*.mxd";
            saveFileDlg.Title = "Save As";

            //Create the mxd
            if (saveFileDlg.FileName != "")
                //Open up the connection to see if the map service already exists.  If it does, this will blow up on AddConfiguration.
                IGISServerConnection agsServerConnection = new GISServerConnection();
                agsServerConnection.Connect(agsServerTxt.Text); //Selected server name.

                //Create the configuration if it doesn't already exist.
                if (serverObjectConfiguration == null)
                    serverObjectConfiguration = agsServerConnection.ServerObjectAdmin.CreateConfiguration() as IServerObjectConfiguration2;

                if (serverObjectConfiguration != null)
                    //Stop the map service or overwriting this will crash.
                    if (bFound)
                        agsServerConnection.ServerObjectAdmin.StopConfiguration(mapServerCbx.Text, "MapServer");

                    //Save the mxd
                    String uncPath = Pathing.GetUNCPath(saveFileDlg.FileName);

                    // Set up the standard properties as defined in our property set
                    serverObjectConfiguration.Description = descriptionTxt.Text;
                    serverObjectConfiguration.IsPooled = true;
                    serverObjectConfiguration.MaxInstances = 20;//Convert.ToInt32(propertySet.GetProperty("MaxInstances"));
                    serverObjectConfiguration.MinInstances = 1;//Convert.ToInt32(propertySet.GetProperty("MinInstances"));
                    serverObjectConfiguration.Name = mapServerCbx.Text;//propertySet.GetProperty("Name").ToString();
                    serverObjectConfiguration.StartupType = esriStartupType.esriSTAutomatic;
                    serverObjectConfiguration.TypeName = "MapServer";

                    // Map Server objects have an additional property to point to the Mxd location
                    IPropertySet configPropertySet = serverObjectConfiguration.Properties;
                    configPropertySet.SetProperty("FilePath", saveFileDlg.FileName);//propertySet.GetProperty("MxdPath").ToString());
                    configPropertySet.SetProperties("OutputDir", "c:\\arcgisserver\arcgisoutput");
                    string virtualOutputDir = " http://" + agsServerTxt.Text + "/arcgisoutput";

                    configPropertySet.SetProperties("VirtualOutputDir", virtualOutputDir);
                    configPropertySet.SetProperties("MaxImageHeight", "2048");
                    configPropertySet.SetProperties("MaxRecordCount", "500");
                    configPropertySet.SetProperties("MaxBufferCount", "100");
                    configPropertySet.SetProperties("MaxImageWidth", "2048");
                    serverObjectConfiguration.Properties = configPropertySet;
                    // We also need to enable basic web access
                    IPropertySet infoPropertySet = serverObjectConfiguration.Info;
                    infoPropertySet.SetProperty("WebEnabled", "true");
                    infoPropertySet.SetProperty("WebCapabilities", "Map,Query,Data");
                    serverObjectConfiguration.Info = infoPropertySet;

                    // Set recycle properties of the map service
                    IPropertySet recycleProperty = serverObjectConfiguration.RecycleProperties;
                    recycleProperty.SetProperties("StartTime", "12:00 AM");
                    recycleProperty.SetProperties("Interval", "86400");  // every 24 hours
                    serverObjectConfiguration.RecycleProperties = recycleProperty;

                    if (!bFound)
                        // Add the configuration to the ArcGIS Server instance
                        // Update the existing configuration in the ArcGIS Server instance

                    //Restart the map service.
                    agsServerConnection.ServerObjectAdmin.StartConfiguration(mapServerCbx.Text, "MapServer");

What am I doing wrong here?  One of our map gurus but non-developers thought it might be something with the active view?  Or is this just the completely wrong workflow?  Another developer suggested programatically copying the feature from one database to another, is that what I need?  And if so, is there an example of that anywhere?

Thanks so much for looking through this, I know it's a ton of information.