Vault API 2016 - how to keep references when renaming files

Vault API 2016 - how to keep references when renaming files

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

Vault API 2016 - how to keep references when renaming files

Anonymous
Not applicable

I am using the following code to rename files in Vault, and it works fine. But, I also want to keep file references during the renaming procedure. What is the way to do that? Thank you.

 

File checkedOutNewFile = CheckOut(folders[0], selectedFile);

if (checkedOutNewFile != null)

{

CheckIn(folders[0], checkedOutNewFile, newname, Coment);

}

 

public File CheckOut(Folder folder, File file)

{

string localPath = _connection.WorkingFoldersManager.GetWorkingFolder(folder.FullName).FullPath;

if (!System.IO.Directory.Exists(localPath))

System.IO.Directory.CreateDirectory(localPath);

AcquireFilesSettings settings = new VDF.Vault.Settings.AcquireFilesSettings(_connection);

settings.OptionsRelationshipGathering.FileRelationshipSettings.VersionGatheringOption = Autodesk.DataManagement.Client.Framework.Vault.Currency.VersionGatheringOption.Latest;

settings.OptionsResolution.OverwriteOption = AcquireFilesSettings.AcquireFileResolutionOptions.OverwriteOptions.ForceOverwriteAll;

settings.LocalPath = new VDF.Currency.FolderPathAbsolute(localPath);

settings.AddFileToAcquire(

new VDF.Vault.Currency.Entities.FileIteration(_connection, file),

VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download | VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Checkout);

VDF.Vault.Results.AcquireFilesResults result = _connection.FileManager.AcquireFiles(settings);

foreach (var r in result.FileResults)

{

 

return m_serviceManager.DocumentService.GetFileById(r.File.EntityIterationId);

 

}

public File CheckIn(Folder folder, File file, string newname,string comm)

{

string localPath = _connection.WorkingFoldersManager.GetWorkingFolder(folder.FullName).FullPath;

string filePath = System.IO.Path.Combine(localPath, file.Name);

VDF.Vault.Currency.Entities.FileIteration ff = _connection.FileManager.CheckinFile(

new VDF.Vault.Currency.Entities.FileIteration(_connection, file), comm, false, null, null,

true, newname, file.FileClass, false,

new VDF.Currency.FilePathAbsolute(filePath));

 

 

return m_serviceManager.DocumentService.GetFileById(ff.EntityIterationId);

 

 

}

 

 

0 Likes
Accepted solutions (1)
2,035 Views
2 Replies
Replies (2)
Message 2 of 3

manuel.lanthaler
Enthusiast
Enthusiast
Accepted solution

Hi,

 

the CheckInFile  function from the Filemanager you are using has a parameter for the FileAssociations. 

Therefore in order to keep the associations you could do the following steps:

  1. use the DocumentService function GetFileAssociationsById  or the GetFileAssociationLite function from the FileManager to get the list of associations
  2. Create and initialize a new List of FileAssocParam
  3. Pass the list of FileAssocs to the CheckInFile function

An example on how to get the fileAssociations and building the list of FileAssocParams can be found here:

http://adndevblog.typepad.com/manufacturing/2013/09/vault-2014-api-example-that-adds-a-file-and-asso...

 

hope it helps

Manuel

coolOrange
www.coolOrange.com
––––––––––––––––––
Please mark this response as "Accept as Solution" if it answers your question.
If you have found any post to be helpful, even if it's not a direct solution, then please provide that author kudos.


0 Likes
Message 3 of 3

Anonymous
Not applicable

Thyanks, Manuel.

 

I used what I think is your code (coolOrange), attached below. Eerything works fine except the last function call updateReferenceModel.UpdateRefsInLocalFile(acquireFilesResult.File, acquireFilesResult.LocalPath, refs); which returns status "NotProcessed" "The reference could not be updated". I wonder what would be the reason for that and how to solve it. The file has been already checked out, so maybe that's the reason. I used your code from here https://github.com/coolOrange-Public/Blog-Samples/blob/master/UpdatingReferencesSample/UpdatingRefer...

 

Thanks,

 

Nebojsa

 

 

Here is the code:

 

public static void UpdateRefsInLocalFile(DocumentService documentService, File file, AcquireFilesResults results)
		{
			foreach (FileAcquisitionResult acquireFilesResult in results.FileResults)
			{
				var assocs = documentService.GetFileAssociationsByIds(new[] { acquireFilesResult.File.EntityIterationId },
																	  FileAssociationTypeEnum.None, false,
																	  FileAssociationTypeEnum.Dependency, false, false, false);
				if (assocs.First().FileAssocs == null)
					continue;
				var fileAssocs = assocs.First().FileAssocs.Where(fa => fa.ParFile.MasterId == acquireFilesResult.File.EntityMasterId);
				var refs = new List<FileReference>();
				foreach (FileAssoc fileAssoc in fileAssocs)
				{
					var fileCld = results.FileResults.FirstOrDefault(f => f.File.EntityMasterId == fileAssoc.CldFile.MasterId);
					if (fileCld == null)
						continue;
					var reference = new FileReference(fileAssoc.RefId, fileCld.LocalPath, fileAssoc.Source);
					refs.Add(reference);
				}

				var updateReferenceModel = new UpdateFileReferencesModel();
				updateReferenceModel.SetTargetFilePath(acquireFilesResult.File, acquireFilesResult.LocalPath);
				updateReferenceModel.UpdateRefsInLocalFile(acquireFilesResult.File, acquireFilesResult.LocalPath, refs);
			}
		}
0 Likes