Getting the file from history on State action

Hi,

I'm trying to go through the history of an object to get a specific file based on the extension and reallocate it to the current version on a state action.

The problem is that I always get different errors.

I've tried creating a new objVerEx and getting the history from there, but I keep getting the error:

- The object has been checked out in M-Files, but the version you are attempting to access is not the current checked-out version. The issue is usually caused by outdated client information.

            Vault vault = env.Vault;
            ObjVerEx obj = env.ObjVerEx;

            var newObjVerEx = new ObjVerEx(env.Vault, obj.ObjVer);
            var b = newObjVerEx.StartRequireCheckedOut();

            var his = obj.History.ToList();

            ObjectFiles objectFilesHistory = obj.Vault.ObjectFileOperations.GetFilesForModificationInEventHandler(his[0].ObjVer);

            int i = 1;

            while (objectFilesHistory[1].Extension.ToLower().Equals("pdf"))
            {
                objectFilesHistory = obj.Vault.ObjectFileOperations.GetFilesForModificationInEventHandler(his[i].ObjVer);
                i++;
            }

            SourceObjectFiles sourceObjectFiles = new SourceObjectFiles
            {
                { -1, Utils.CreateSourceFile(env, objectFilesHistory) }
            };

            newObjVerEx.ReplaceFiles(sourceObjectFiles);
            newObjVerEx.EndRequireCheckedOut(b, env.CurrentUserID);

            File.Delete(sourceObjectFiles[1].SourceFilePath);

The code at the moment is a draft because of all the tests I've done.

I check it out, but I'm pretty sure that since I'm in a state action, I'm already checked out.

Should I do a synchrone task for this matter ? Or is there a way of achieving my goal here ?

What I want is the possibility to go through all version files and get the one I want and set it as the current/new version file.