Renaming File via Document Relationship - A journey

Hi Community,

Need some help here. I have a method to rename a file related to a document. The idea is that upon check-in the filename will update to the Doc Number + Name + Rev + Generation

It works for the most part but there are 4 errors depending on how this is implemented.

1. When set to OnBeforeLock/OnAfterLock, the code to define the string for the name only takes one of the values I added for concatenation and does not actually get the values from this.getProperty("item_number"), etc. so it ends up naming every file "v"

2. When set to OnAfterUpdate or OnAfterAdd I get an error when saving via the office connector, of note, OnAfterUpdate works fine when making an edit to the form, at that point the filename does get updated correctly: 

3. When set to OnAfterVersion, it works fine except when storing a document via the Office Connector...instead of replacing the files, it adds them to the previous versions. This causes problems. It also means the filename is not correct until the vile is versioned.

4. When set to OnBeforePromote/OnAfterPromote/OnBeforeAdd:

Help...

  • This one requires some background information first.

    1. Do you use the Aras Office Connector?

    2. Why do you want to use all available Method events types (onBeforeLock??)? Do you really want to rename your document every time "something" happens?

    3. onAfterAdd/Update/Version should do the job. Which code do you use for updating the item?

  • Hello,

    I only want to use one event type if possible. We are using the connector, however I attempted each type due to the fact that none of them were able to complete the task without issue. The closest and most ideal would be OnAfterUpdate however the office connector throws the error.

    Here is the code:

    Innovator inn = this.getInnovator();
    string sourceId = this.getID();
    
    String relationshipName;
    //,souceProperty, sourceContentLocation;
    
    //Item queryFile, 
    Item item, files, file;
    
    //filetype or doc validation if reuqired
    if (true) {
        relationshipName = "Document File";
    
        item = this.newItem("Document", "get");
        item.setProperty("id", sourceId);
        Item isRelationship = this.newItem(relationshipName, "get");
        item.addRelationship(isRelationship);
        item = item.apply();
    
    //get all Documet Files 
        files = item.getRelationships(relationshipName);
        
        //To iterate through attached files
        for (int i = 0; i < files.getItemCount(); i++) {
            file = files.getItemByIndex(i);
            file = file.getRelatedItem();
    
    //string to be added into filename
            string stringToRenameFile = this.getProperty("item_number") + " "+ this.getProperty("name") + " " + this.getProperty("major_rev") + "v" + this.getProperty("generation");
    
            string fileId = file.getID();
            string fname = file.getProperty("filename");
    
            string[] array = fname.Split('.');
            fname = string.Empty;
            if (array.Length == 1) {
                fname = stringToRenameFile + array[1];
            } else {
                for (int j = 0; j < array.Length; j++) {
                    fname += array[j];
                    if (j == array.Length - 2)
                    if (j != array.Length - 1)
                    {
                        fname = stringToRenameFile;
                fname = stringToRenameFile + ".";
                    }
                        }
            }
            file = this.newItem("File");
            file.setAttribute("action", "RenameFile");
            file.setID(fileId);
            file.setProperty("filename", fname);
    
            // to get root user permission via identity
            Aras.Server.Security.Identity identity = Aras.Server.Security.Identity.GetByName("Super User");
            bool permsWasSet = Aras.Server.Security.Permissions.GrantIdentity(identity);
    
            file = file.apply();
    
            //acquired root user permission should be revoked
            if (permsWasSet)
                Aras.Server.Security.Permissions.RevokeIdentity(identity);
        }
    }
    return this;

  • I figured it out. Replaced this with item and all is well with onBeforeUnlock.