This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Replace existing file

Hey, 

I am developing a VAF that I would like to replace an existing file with a new file. I am using the below code but I am having some issues. 

I firstly Check out the existing document (ReplacePDF) I am calling GetFilesForModificationInEventHandler, then calling ReplaceFiles and finally Check in the document. 

The file newObjectSourceFiles is the new file that I want to replace. 

var newObjVer = env.Vault.ObjectOperations.CheckOut(ReplacePDF.ObjVer.ObjID);
env.Vault.ObjectFileOperations.GetFilesForModificationInEventHandler(newObjVer.ObjVer);

env.ObjVerEx.ReplaceFiles(newObjectSourceFiles);
env.Vault.ObjectOperations.CheckIn(newObjVer.ObjVer);

Thank you

  • Forgot to add the error that I am getting: 

    The script is attempting to modify the files of an object that has been automatically checked out, but the script has not called the "GetFilesForModificationInEventHandler" method. Automatic checkout occurs when the properties of an object are modified without explicitly checking out the object.

    The script must call the "GetFilesForModificationInEventHandler" method prior to modifying the files of the object via such operation. (0x8004084C)

  • The code checks out the object that "ReplacePDF" points to, but it seems to alter the object that "env.ObjVerEx" points to.  Which object should have the files replaced on it?

  • I want to replace ReplacePDF with the newObjectSourceFiles

  • var objVerEx = new ObjVerEx(env.Vault, ReplacePDF.ObjVer);
    var b = objVerEx.StartRequireCheckedOut();
    objVerEx.ReplaceFiles(newObjectSourceFiles);
    objVerEx.EndRequireCheckedOut(b, env.CurrentUserID);

    In that case you need to get a new ObjVerEx representing the object you want to work with, and operate on that.

  • That worked! Great thanks 

  • Following your solution Craig I am now trying for the replaced document to move it to  a specific workflow state. 

    I tried using after the below line : 

    objVerEx.EndRequireCheckedOut(b, env.CurrentUserID);

    This: 

    objVerEx.SetWorkflowState(WorkflowAlias, StateID);

    But nothing happens.

  • Hi Stavros,

    You need to make sure that you make any changes to properties (such as setting the workflow state), then save them to the vault, then check the object back in.  It must be in that order, same as in VBScript or anything else.

  • Hey Craig, 

    So I guess the below order is not correc as is not working? 

    objVerEx.ReplaceFiles(newObjectSourceFiles);
    objVerEx.SetWorkflowState(WorkflowAlias, StateID);

    Do I have to do this before the ReplaceFiles method, at the poing I collect this document? 

    Because tried this and not working for a property:

    var PV = new PropertyValue();
    PV.PropertyDef = 1093;
    PV.TypedValue.SetValue(MFDataType.MFDatatypeText, "Change test 123");

  • The order is always:

    1. Check the object out
    2. Make changes
    3. Check it back in

    Numbers 1 and 3 may sometimes be implicit (e.g. if changing the current object in an event handler then it's already checked out and you don't need to check it in), but in general the above steps need to be taken.  Both replacing files and setting properties (e.g. workflow) need to happen before the object is checked in.

    In the specific code sample you posted you seem to be creating a property value in memory, but I can't see how you're setting it back to the vault.  So this code on its own won't do anything at a practical level.

  • I am using EndRequireCheckedOut(). Isn't this the same with check out? 

    Anyway I used both ways but still did not work : 

    var objVerEx = new ObjVerEx(env.Vault, ReplacePDF.ObjVer);
    var b = objVerEx.StartRequireCheckedOut();
    objVerEx.ReplaceFiles(newObjectSourceFiles); //Replace the file 
    objVerEx.SetWorkflowState("WF.HsqeWorkflow", 132); //Change the wokflow 
    objVerEx.SetProperty("PD.Company", MFDataType.MFDatatypeMultiSelectLookup, 673); //Set up a property
    objVerEx.CheckIn(); //Check In the object 

    Also tried for the last line to use but did not work. It does nothing. 

    objVerEx.EndRequireCheckedOut(b, env.CurrentUserID);