Using ArcObjects to retrieve all versions of ArcSDE in C#

Recently, I needed to get all the existing versions of an ArcGIS database (sde.DEFAULT, and so on). To do so, I decided to use ArcObjects in C#.NET. I found it pretty complicated to manage to get what I wanted to do, so I’m placing my code on this blog.

First, you need to connect to a valid license.

ESRI.ArcGIS.esriSystem.esriLicenseStatus eLicenseStatus = init.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);

if (eLicenseStatus == ESRI.ArcGIS.esriSystem.esriLicenseStatus.esriLicenseAvailable)
{
init.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
}

Then,  connect to the ArcSDE Service.

ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactory();
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = null;

ESRI.ArcGIS.esriSystem.PropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
propertySet.SetProperty(“server”, m_ConnectionInformationData.ServerName);
propertySet.SetProperty(“database”, m_ConnectionInformationData.DatabaseName);
propertySet.SetProperty(“user”, m_ConnectionInformationData.UserName);
propertySet.SetProperty(“password”, m_ConnectionInformationData.UserPassword);
propertySet.SetProperty(“instance”, m_ConnectionInformationData.InstanceName);
propertySet.SetProperty(“version”, m_ConnectionInformationData.Version);

pWorkspace = pWFactory.Open(propertySet, 0);

Cast your workspace to a VersionedWorkspace:

ESRI.ArcGIS.Geodatabase.IVersionedWorkspace versionedWorkspace = (ESRI.ArcGIS.Geodatabase.IVersionedWorkspace)pWorkspace;

You can then iterate through all the versions.

//get a enumeration of all the versions on the versioned workspace
ESRI.ArcGIS.Geodatabase.IEnumVersionInfo enumVersionInfo = versionedWorkspace.Versions;
enumVersionInfo.Reset();

cbxVersion.Items.Clear();

ESRI.ArcGIS.Geodatabase.IVersionInfo versionInfo = enumVersionInfo.Next();
while (versionInfo != null)
{
string versionName = versionInfo.VersionName;
cbxVersion.Items.Add(versionName);
versionInfo = enumVersionInfo.Next();
}

Finally, don’t forget to close the connection.

init.Shutdown();

Here is what it looks like.

You can find an official example at the ESRI Resources Center.

Follow

Get every new post delivered to your Inbox.