By Daniel Du
I am playing with the Vault Development Framwork(VDF) which is provided from Autodesk Vault 2014, here is a demo of getting file’s properties with VDF. I am doing in C#, and I just noticed that Wayne did a similar sample in VB.net here.
In Vault 2013 or earlier version, we can get properties with PropertyService PropertyManager. While in Vault 2014, with VDF we have a new way to do that, we can get the properties with PropertyManager, following code snippet demos how to login to Vault and get property manager. With VDF, it is easy to login vault with a built-in well designed UI, or a non-UI way if prefer:
////login with UI //VDF.Vault.Currency.Connections.Connection connection = VDF.Vault.Forms.Library.Login(new VDF.Vault.Forms.Settings.LoginSettings()); //Another way to log into Vault without UI VDF.Vault.Results.LogInResult results = VDF.Vault.Library.ConnectionManager.LogIn( "localhost", "Vault", "Administrator", "", VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, null); if (!results.Success) { Console.WriteLine("Login failed. exit..."); Console.ReadLine(); return; } VDF.Vault.Currency.Connections.Connection connection = results.Connection; VDF.Vault.Services.Connection.IPropertyManager propMgr = connection.PropertyManager;
In this sample, I firstly get all folders/file entities in vault, talking about this, the first idea comes to my head is to use FolderManager and FileManager, but with help of Daniel Dulzo, one of my Vault API expert colleauges, I will use
IEntityOperationManager, which can be accessed off of a connection object through the EntityOperations property, provides a set of methods that can be used on generic IEntity instances. So while I checked for a method to get child files in both the FileManager and the FolderManager, the method to get child entities actually exists on the IEntityOperationManager because it is generic to all entities.
OK, here are the code snippet:
using Autodesk.Connectivity.WebServices;
using System;
using System.Collections.Generic;
using System.Linq;
using VDF = Autodesk.DataManagement.Client.Framework;
namespace VaultLabs
{
class Lab03
{
// We will collect Property Definitions here
static VDF.Vault.Currency.Properties.PropertyDefinitionDictionary propDefs;
// The entry point of the program
//==========================================================================
static void Main(string[] args)
{
try
{
////login with UI
//VDF.Vault.Currency.Connections.Connection connection
= VDF.Vault.Forms.Library.Login(new VDF.Vault.Forms.Settings.LoginSettings());
//Another way to log into Vault without UI
VDF.Vault.Results.LogInResult results =
VDF.Vault.Library.ConnectionManager.LogIn(
"localhost", "Vault", "Administrator", "",
VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, null);
if (!results.Success)
{
Console.WriteLine("Login failed. exit...");
Console.ReadLine();
return;
}
VDF.Vault.Currency.Connections.Connection connection = results.Connection;
if (connection.IsConnected)
{
ReadProperties(connection);
VDF.Vault.Currency.Entities.Folder folder = connection.FolderManager.RootFolder;
PrintChildren(connection, folder);
Console.ResetColor();
Console.WriteLine("");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
}
} // Main()
// Read all the Property Definitions for the "FILE" Entity Class
//===============================================================================
static void ReadProperties(VDF.Vault.Currency.Connections.Connection connection)
{
propDefs =
connection.PropertyManager.GetPropertyDefinitions(
VDF.Vault.Currency.Entities.EntityClassIds.Files,
null,
VDF.Vault.Currency.Properties.PropertyDefinitionFilter.IncludeUserDefined
);
}
// Output information about each file in a folder along with its properties
//===========================================================================
static void PrintChildren(VDF.Vault.Currency.Connections.Connection connection,
VDF.Vault.Currency.Entities.Folder parentFolder)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("{0}", parentFolder.FullName);
IEnumerable<VDF.Vault.Currency.Entities.IEntity> entities = connection
.EntityOperations.GetBrowseChildren(parentFolder);
if (entities != null && entities.Count<VDF.Vault.Currency.Entities.IEntity>() > 0)
{
foreach (var ent in entities)
{
if (ent is VDF.Vault.Currency.Entities.FileIteration)
{
VDF.Vault.Currency.Entities.FileIteration fileIteration
= ent as VDF.Vault.Currency.Entities.FileIteration;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" {0}", fileIteration.EntityName);
Console.ForegroundColor = ConsoleColor.Yellow;
//Now print the properties of the file
PrintProperties(connection, fileIteration);
}
else if (ent is VDF.Vault.Currency.Entities.Folder)
{
// Recursively print info about subfolders and files in them
//-------------------------------------------------------------------------
VDF.Vault.Currency.Entities.Folder folder
= ent as VDF.Vault.Currency.Entities.Folder;
PrintChildren(connection, folder);
}
}
}
}
static void PrintProperties(VDF.Vault.Currency.Connections.Connection connection,
VDF.Vault.Currency.Entities.FileIteration fileInteration)
{
foreach (var key in propDefs.Keys)
{
// Print the Name from the Definition and the Value from the Property
object propValue = connection.PropertyManager.GetPropertyValue(
fileInteration, propDefs[key], null);
Console.WriteLine(" '{0}' = '{1}'",
key.ToString(),
propValue == null ? "" : propValue.ToString());
}
}
} // class Lab03
} // namespace VaultLabs