Programmatically checkout, overwrite and check in file

Programmatically checkout, overwrite and check in file

Anonymous
Not applicable
3,011 Views
2 Replies
Message 1 of 3

Programmatically checkout, overwrite and check in file

Anonymous
Not applicable

I'm writing an API in C# .NET. What I need to do is: 

1. Checkout file

2. Overwrite it with local file

3. Check the file back in with a comment and incremented version number.

 

I've tried different ways and nothing works. 

 

What methods should I use?

 

I tried the following

 

1. Acquire file and CheckoutFile.  This part works just fine.
2. I think I can overwrite the file using .NET File.Copy

3. I'm lost. I tried CheckInFile and get 8000 error, I tried CheckinUploadedFile with checkout ticket and with new ticket.

 

Does anyone have any suggestions?

 

0 Likes
3,012 Views
2 Replies
Replies (2)
Message 2 of 3

Markus.Koechl
Autodesk
Autodesk

This code snippet should share all you need, except the override of the downloaded file (Step 2.)

VDF.Vault.Settings.AcquireFilesSettings aqSettings = new VDF.Vault.Settings.AcquireFilesSettings(connection)
                        {
                            DefaultAcquisitionOption = VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Checkout
                        };
                        vdfFile = new VDF.Vault.Currency.Entities.FileIteration(connection, wsFile);
                        aqSettings.AddEntityToAcquire(vdfFile);
                        var results = connection.FileManager.AcquireFiles(aqSettings);
                        try
                        {
                            mUploadedFile = connection.FileManager.CheckinFile(results.FileResults.First().File, "Created by Job Processor", false, null, null, false, null, ACW.FileClassification.DesignRepresentation, false, vdfPath);
                            mExpFile = mUploadedFile;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Job could not update existing export file " + vdfFile + "Exception: ", ex);
                        }

 You see the context here is a custom job, but this doesn't matter. If you need more insights on the step required before goto the full source: https://github.com/koechlm/Vault-Sample---InventorExportAnySampleJob.

 

Need to know: this code applies to non-CAD files only. CAD files may have BOM information that needs to be updated or at least kept for the new file version.



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
Message 3 of 3

Anonymous
Not applicable

I tried this version and it seems to work.

 

Step 1. Checkout file:

ByteArray downloadTicket;

Autodesk.Connectivity.WebServices.File downloadResult = _connection.WebServiceManager.DocumentService.CheckoutFile(
                       file.Id,
                       CheckoutFileOptions.Master,
                       Environment.MachineName,
                       path,
                       comment,
                       out downloadTicket
                    );

 

Step 2:  Read new file contents into ByteArray:

 

//  Get contents of file
using (var stream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
{
      byte[] bytes = new byte[stream.Length];
      int numBytesToRead = (int)stream.Length;
      int numBytesRead = 0;

      while (numBytesToRead > 0)
      {
            int n = stream.Read(bytes, numBytesRead, numBytesToRead);
            if (n == 0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
       }
       stream.Close();

       //  Upload file to Vault
       checkinByteArray = UploadFileResource(sourceFilePath, bytes);
}

 

Step 3: Upload file to Vault. This method I found on this forum.

private ByteArray UploadFileResource(string filename, byte[] fileContents)
{            _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue = new FileTransferHeader();
                _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.Identity = Guid.NewGuid();
                _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.Extension = Path.GetExtension(filename);
                _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.Vault = _connection.WebServiceManager.WebServiceCredentials.VaultName;

                ByteArray uploadTicket = new ByteArray();
                int bytesTotal = (fileContents != null ? fileContents.Length : 0);
                int bytesTransferred = 0;
                do
                {
                    int bufferSize = (bytesTotal - bytesTransferred) % maxTransferSize;
                    byte[] buffer = null;
                    if (bufferSize == bytesTotal)
                    {
                        buffer = fileContents;
                    }
                    else
                    {
                        buffer = new byte[bufferSize];
                        Array.Copy(fileContents, (long)bytesTransferred, buffer, 0, (long)bufferSize);
                    }

                    _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.Compression = Compression.None;
                    _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.IsComplete = (bytesTransferred + bufferSize) == bytesTotal ? true : false;
                    _connection.WebServiceManager.FilestoreService.FileTransferHeaderValue.UncompressedSize = bufferSize;

                    using (var fileContentsStream = new MemoryStream(fileContents))
                        uploadTicket.Bytes = _connection.WebServiceManager.FilestoreService.UploadFilePart(fileContentsStream);
                    bytesTransferred += bufferSize;

                } while (bytesTransferred < bytesTotal);

                return uploadTicket;
            }                    
}        


Step 4:  Check in file

 Autodesk.Connectivity.WebServices.File checkInResult = _connection.WebServiceManager.DocumentService.CheckinUploadedFile(
                   file.MasterId,
                   comment,
                   false,
                   DateTime.UtcNow,
                   fileAssociations,
                   null,
                   false,
                   null,
                   file.FileClass,
                   file.Hidden,
                   checkinByteArray);
0 Likes