For future reference.
There is a way, but it doesn't mantain the symbology. You'll have to publish a GPServer to your Arcgis Server to make it happen. It's not possible to do this using only javascript.
I've done using arcgis js api 3.x (to create a custom widget for web appbuilder) and a python GPServer. Here is how you can develop it:
The python script part:
1. Create a toolbox using Arcmap (or Arcgis Pro, if you're already using it) and publish it as a GPServer. The toolbox must contain a python script using arcpy with the following instructions:
1.2 Toolbox should have 2 input parameters: one for File and the other for the geometry type.
1.3 Toolbox should have 1 output parameter. The output will be a json file to be used in arcgis js api.
1.4 Check input preconditions:
1.4.1 File extension must exist and must have .dwg extension.
1.4.2 Geometry type must exist and must be polygon, point or polyline.
1.5 Use the arcpy.FeatureClassToShapefile_conversion(pathToTheInputDWGFile, tempDirectory) to convert the DWG file to Shapefile. This is a required step.
1.6 Use arcpy.FeaturesToJSON_conversion(pathToShapeFileInTheTempDirectory, pathToResultJsonFile, "NOT_FORMATTED", "NO_Z_VALUES", "NO_M_VALUES", "NO_GEOJSON")
1.7 Use arcpy.SetParameterAsText(2, pathToResultJsonFile) to send the converted file as json to your arcgis js api.
The javascript part:
2. Develop a Widget using Arcgis JS api (3.x or 4.x, it doesn't matter) with the following inputs:
2.1 - A select box containing the geometry type it is supposed to load from the DWG file: Point, Polyline or Polygon.
2.2 - Input field to add the DWG file.
2.3 - A layer name, to be added to the map.
2.4 - Query the GPServer using esriRequest module and use following code to create a featureLayer from the gpServerJsonResult:
For arcgis js api 4.x, you can code something like this:
var featureSet = FeatureSet.fromJSON(gpServerJsonResult);
var dwgLayer = new FeatureLayer({
title: featureTitle,
id: "aDwgLayer",
source: featureSet.features,
fields: featureSet.fields,
objectIdField: featureSet.fields[0].name,
spatialReference: featureSet.spatialReference,
geometryType: featureSet.geometryType,
renderer: yourCustomRenderer
});
map.addLayer(dwgLayer);
For arcgis js api 3.x, you can code something like this:
var featureCollection = {
layerDefinition: gpServerJsonResult,
featureSet: {
features: gpServerJsonResult.features,
geometryType: gpServerJsonResult.geometryType
}
};
var dwgLayer = new FeatureLayer(featureCollection);
map.addLayer(dwgLayer);
You can find more ways to create the python script to convert CAD data to Arcgis shapefile here:
How To: Convert selected CAD data to ArcGIS shapefile, coverage, or geodatabase feature classes usin... . However, what worked for me was arcpy.FeatureClassToShapefile_conversion tool.