If you want to dynamically add data to your map service, such as feature classes, shapefiles, rasters, or feature attribute tables, you will need to set up a workspace to contain the data you want to add. Dynamic workspaces can be any enterprise geodatabase, file geodatabase, or file directory on disk that is accessible to the server. If your dynamic workspace is an enterprise geodatabase, you have the option of limiting user access to the geodatabase by locking the database version.
Let's discuss why the ability to dynamically add data to a map service is so useful. First consider that, for performance reasons, it is generally good practice to keep the number of layers in a dynamic map service to a minimum. With this said, there may be circumstances when a use-case may warrant the ability to work with multiple layers in a map service. This would be a good opportunity to take advantage of dynamic workspaces and their ability to add data to the map service on-the-fly.
For example, consider you have a service with basic layers such as states, provinces, counties, rivers, and/or cities. These are used as template base layers for your service. You may have requirements that call for adding potentially hundreds of various layers to the template base layers. Prior to ArcGIS 10.1, you would have had to put all of these layers in the source document that was used to create the service. Now all you need to do is make certain that the service allows dynamic capability with per-request modification of layer order and symbology.
You can then add data, as in the above example where the 'hundreds of layers', would reside in this registered workspace. When you add a dynamic workspace, you provide the workspace type, id, and location. Once you have configured these settings, you then have the capability to add data from the registered location to your map service dynamically. See the ArcGIS Help for more information on how to do configure these settings.
Dynamically adding layers at runtime
The Dynamic workspace sample illustrates how to dynamically add a layer at runtime. This sample uses the ArcGISDynamicMapServiceLayer's layerDrawingOptions property in addition to its createDynamicLayerInfosFromLayerInfos method. This method returns an array of DynamicLayerInfo instances. The sample gets the dynamic layer info array from the dynamic map service, it then writes back to this array with a new DynamicLayerInfo object. After this, the layerDrawingOptions property is written to that of a new layerDrawingOptions instance. Snippet taken from Dynamic workspace sample
var dynamicLayerInfos:Array = usaLayer.createDynamicLayerInfosFromLayerInfos();
usaLayer.dynamicLayerInfos = dynamicLayerInfos;
usaLayer.layerDrawingOptions = [ lakesLayerDrawingOptions ];
Snippet taken from Dynamic workspace sample
The DynamicLayerInfo's source property is set to an instance of a LayerDataSource. The LayerDataSource's dataSource property is set to an instance of a TableDataSource. This TableDataSource contains all of the dynamic workspace information. The dataSourceName is the name of the layer to be added. The workspaceId is the unique workspace identifier that was configured when enabling the dynamic workspace.
You must know the name of the layers you are adding. In addition, the ArcGIS Server account must have at least read access to the dynamic workspace that contains the data you wish to dynamically add to the map service.
Dynamic workspaces are enabled outside of the web APIs using ArcGIS for Desktop or Manager. More information on setting the map service properties can be found at Setting map service properties. See Making your data accessible to ArcGIS Server for additional information regarding setting permissions on these workspaces.