Possible to edit a version programmatically?

1719
2
12-13-2013 12:16 PM
MarkHawkins
New Contributor
Does anyone know if it is possible to edit a version programmatically? Below is some sample code where I locate a users version and attempt to create a new feature in it. It works without any errors but the new features always end up in the default version. I've also tried opening the workspace from a sde connection file that is set to a specific version but the result is the same.. changes are applied to the default version.

The only place I find mention of this is in a thread from EDN 9.2 and prior where it describes versions as database states and says that it isn't possible to edit the versions directly and talks about editing states, etc.

http://edndoc.esri.com/arcsde/9.1/java_api/overview/editing_versions.htm

            IWorkspaceFactory2 WrkSpcFact = new SdeWorkspaceFactoryClass();
            
            IWorkspace WrkSpc;
     WrkSpc = WrkSpcFact.OpenFromFile("\\MyConnection.sde", 0);
  
            IVersionedWorkspace3 VrsWrkSpc;
            VrsWrkSpc = WrkSpc as IVersionedWorkspace3;

     //get version for current system user
            IEnumVersionInfo VerEnum = VrsWrkSpc.Versions;
            IVersionInfo VersInfo = VerEnum.Next();
            string UserID = Environment.UserName.ToUpper();
            string VersionName;
            string TargetVersionName = "";
            IVersion TargetVersion = null;
            while (VersInfo != null)
            {
                VersionName = VersInfo.VersionName;
                VersionName = VersionName.ToUpper();
                if (VersionName.Contains(UserID))
                {
                    TargetVersionName = VersInfo.VersionName;
                    TargetVersion = VrsWrkSpc.FindVersion(VersInfo.VersionName);       
                }
                VersInfo = VerEnum.Next();
            }
            

     //create workspace from version
     IFeatureWorkspace FeatWrkSpc;
     FeatWrkSpc = (IFeatureWorkspace)TargetVersion;
            IFeatureClass SitePlanFeatClass = FeatWrkSpc.OpenFeatureClass("BASE_ADMIN.site_plan");

            //start SDE versioned editing session
            IWorkspaceEdit VrsWrkSpcEdit = (IWorkspaceEdit)FeatWrkSpc;
            IMultiuserWorkspaceEdit MUVrsWrkSpcEdit = (IMultiuserWorkspaceEdit)VrsWrkSpcEdit;
            
     MUVrsWrkSpcEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
            VrsWrkSpcEdit.StartEditOperation();

            IFeature NewSitePlanFeature = SitePlanFeatClass.CreateFeature();
            NewSitePlanFeature.Shape = NewPolygonFeat.ShapeCopy;

            //populate some fields, etc

            //save
            NewSitePlanFeature.Store();

            VrsWrkSpcEdit.StopEditOperation();
            VrsWrkSpcEdit.StopEditing(true);
0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
Yes, you can edit any version for which you have permissions to make edits.  This line here determines which version you are connecting to:
WrkSpc = WrkSpcFact.OpenFromFile("\\MyConnection.sde", 0);

You should specify the version you want to connect to when creating the *.sde file (or in the property set if you want to connect using the Open method).
0 Kudos
MarkHawkins
New Contributor
Thanks for the input Neal. I swapped some of the interfaces for the newer versions: IVersion2, IWorkspaceEdit2, etc. and also checked to make sure all my test feature classes were registered as versioned. It is working now.

Thanks,

Mark
0 Kudos