How can I modify a Layer File's data source using ArcObjects?

4276
7
Jump to solution
03-26-2012 11:36 AM
JoshV
by
Occasional Contributor
Greetings,

I'm building an Add-in that lets users change datasources of all MXD's and Layer files in a directory of their choice.  I have the MXD part working but I cannot figure out the Layer file part.  In my code below, I've gathered a list of Layer files in this directory but now I need to open each layer file and change the data source information.  Can anyone shed some light on this?  Many thanks

//Now Loop through LYR Files if there are any and Process them..              for (int y = 0; y < LYList.Count; y++)             {                  string test = LayerFileLocation + "\\" + LYList.ToString();                 ILayerFile l = new LayerFileClass();                 l.Open(LayerFileLocation + "\\" + LYList.ToString());                  //I'm not sure what to do with a Layer file from here.  With an MXD I used the below code to loop through each layer and process it using IMapAdmin2.FireChangeFeatureclass()               }


Here is what I did for MXD's..
UID pUID = new UIDClass();                 pDoc.Open(MXDLocation + "\\" + AL.ToString());//+ fi.Name);                                  for (int i = 0; i < pDoc.MapCount; i++)                 {                     pMap = pDoc.get_Map(i);                      UID uid = new UIDClass();                     //uid.Value = "{EDAD6644-1810-11D1-86AE-0000F8751720}"; //  IGroupLayer                     uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //IFeatureLayer                     IEnumLayer layers = pMap.get_Layers(uid, true);       //Returns all layers including layers in grouplayers                     layers.Reset();                      pFeatureLayer = layers.Next() as IFeatureLayer;                     while (pFeatureLayer != null)                     {...Etc..etc
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
You check the type of the layer.  In C# that's the Is operator.

If (layer Is IGroupLayer) then...

You can access the layers within a group layer using the ICompositeLayer interface.  For your code to be truly robust, you'll need to handle the possibility that group layers can contain other group layers so you can't just simply loop through the layers.  You'll need to write a routine that loops through the layers within the group layer and updates the datasources of the feature layers and calls itself recursively for the group layers.  As for changing the datasource, that's done the same way you did it for the mxd.

View solution in original post

0 Kudos
7 Replies
JeffMatson
Occasional Contributor III
Did you already try ILayerFile.ReplaceContents?

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000004v000000



Greetings,

I'm building an Add-in that lets users change datasources of all MXD's and Layer files in a directory of their choice. I have the MXD part working but I cannot figure out the Layer file part. In my code below, I've gathered a list of Layer files in this directory but now I need to open each layer file and change the data source information. Can anyone shed some light on this? Many thanks

//Now Loop through LYR Files if there are any and Process them..

            for (int y = 0; y < LYList.Count; y++)
            {

                string test = LayerFileLocation + "\\" + LYList.ToString();
                ILayerFile l = new LayerFileClass();
                l.Open(LayerFileLocation + "\\" + LYList.ToString());

                //I'm not sure what to do with a Layer file from here.  With an MXD I used the below code to loop through each layer and process it using IMapAdmin2.FireChangeFeatureclass()


            }


Here is what I did for MXD's..
UID pUID = new UIDClass();
                pDoc.Open(MXDLocation + "\\" + AL.ToString());//+ fi.Name);
                
                for (int i = 0; i < pDoc.MapCount; i++)
                {
                    pMap = pDoc.get_Map(i);

                    UID uid = new UIDClass();
                    //uid.Value = "{EDAD6644-1810-11D1-86AE-0000F8751720}"; //  IGroupLayer
                    uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //IFeatureLayer
                    IEnumLayer layers = pMap.get_Layers(uid, true);       //Returns all layers including layers in grouplayers
                    layers.Reset();

                    pFeatureLayer = layers.Next() as IFeatureLayer;
                    while (pFeatureLayer != null)
                    {...Etc..etc
0 Kudos
JoshV
by
Occasional Contributor
Did you already try ILayerFile.ReplaceContents?

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000004v000000


Hi Jeff-

If I'm understanding ILayer.ReplaceContents correctly then it requires a ILayer which is fine but I think I need to get a list of layers in the LayerFile first right (similiar to MXD)?  Or is there a better approach to this?
0 Kudos
NeilClemmons
Regular Contributor III
With a layer file, you open it using ILayerFile and get the layer reference from the Layer property.  Only one layer can be stored in a layer file.  However, this layer may be a group layer so you'll need to handle that possibility.
0 Kudos
JoshV
by
Occasional Contributor
With a layer file, you open it using ILayerFile and get the layer reference from the Layer property.  Only one layer can be stored in a layer file.  However, this layer may be a group layer so you'll need to handle that possibility.


Hi Neil, thanks for the quick response.

ILayerFile was what I was using so I'm glad I was going in the right direction.  2 questions: Using the code I have above for the LYR file, how can I modify that code to see what the Data Source is of the Layer in the LYR file and then change it to an alternate Data Source?  Lastly, if a group layer exist, how can I loop through that group layer to perform the above question?

Thanks Neil.
0 Kudos
JoshV
by
Occasional Contributor
With a layer file, you open it using ILayerFile and get the layer reference from the Layer property.  Only one layer can be stored in a layer file.  However, this layer may be a group layer so you'll need to handle that possibility.


I got the piece of code working to update the Layer file if it is just one Layer using one of your older post (http://forums.esri.com/Thread.asp?c=159&f=1707&t=303925)(thanks).  Haven't quite figured out how to check the Layer file to see if it's a group layer and then update each layer accordingly.  In my MXD code I was easily able to do this using IMap.get_Layers()
0 Kudos
NeilClemmons
Regular Contributor III
You check the type of the layer.  In C# that's the Is operator.

If (layer Is IGroupLayer) then...

You can access the layers within a group layer using the ICompositeLayer interface.  For your code to be truly robust, you'll need to handle the possibility that group layers can contain other group layers so you can't just simply loop through the layers.  You'll need to write a routine that loops through the layers within the group layer and updates the datasources of the feature layers and calls itself recursively for the group layers.  As for changing the datasource, that's done the same way you did it for the mxd.
0 Kudos
JoshV
by
Occasional Contributor
You check the type of the layer.  In C# that's the Is operator.

If (layer Is IGroupLayer) then...

You can access the layers within a group layer using the ICompositeLayer interface.  For your code to be truly robust, you'll need to handle the possibility that group layers can contain other group layers so you can't just simply loop through the layers.  You'll need to write a routine that loops through the layers within the group layer and updates the datasources of the feature layers and calls itself recursively for the group layers.  As for changing the datasource, that's done the same way you did it for the mxd.


That's perfect Neil.  Thanks again.
0 Kudos