Retrieving Values From Datacard via API

Hello everyone, This is my first post.

I am trying to get variable values from a data card. i have ran into this problem where it returns as “edit”. which i think it’s because it’s user inputed as a text box. I need the value from said text box. Any Thoughts?

Code and Output listed below

using System;
using EPDM.Interop.epdm;
using System.Collections.Generic;

namespace PDMFileDetails
{
    class Program
    {
        private static IEdmFolder5 ppoRetParentFolder;

        [STAThread]
        private static void Main(string[] args)
        {
            string vaultName = "PDM Sandbox";  // Replace with your actual vault name
            string filePath = @"c:\PDM Sandbox\Document Control\IWO Files\13---\13010_IWO.xlsx";  // Adjust this to match your file path

            try
            {
                IEdmVault5 vault = new EdmVault5Class();
                vault.LoginAuto(vaultName, 0);

                if (vault.IsLoggedIn)
                {
                    Console.WriteLine($"Connected to vault: {vaultName}");

                    IEdmFile5 file = vault.GetFileFromPath(filePath, out ppoRetParentFolder);

                    if (file != null)
                    {
                        string stateName = file.CurrentState.Name;
                        Console.WriteLine($"File: {file.Name}, State: {stateName}");

                        // Get and print the specific PROJ_ID value
                        string projID = GetVariableValue(file, "PROJ_ID");
                        Console.WriteLine($"PROJ_ID: {projID}");
                    }
                    else
                    {
                        Console.WriteLine($"File not found: {filePath}");
                    }
                }
                else
                {
                    Console.WriteLine("Failed to log in to the vault.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }

        public static string GetVariableValue(IEdmFile5 file, string variableName)
        {
            if (file == null)
                throw new ArgumentNullException(nameof(file));

            if (string.IsNullOrEmpty(variableName))
                throw new ArgumentNullException(nameof(variableName));

            var vault = file.Vault;

            var extension = System.IO.Path.GetExtension(file.Name).Replace(".", "");

            var aCard = (IEdmCard6)(file.GetNextFolder(file.GetFirstFolderPosition())).GetCard(extension);

            var controlPosition = aCard.GetFirstControlPosition();

            while (controlPosition.IsNull == false)
            {
                IEdmVariable5 variable = null;
                var control = aCard.GetNextControl(controlPosition) as IEdmCardControl7;
                var variableID = control.VariableID;

                if (variableID != 0)
                    variable = vault.GetObject(EdmObjectType.EdmObject_Variable, control.VariableID) as IEdmVariable5;

                if (variable != null)
                {
                    if (variable.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase))
                    {
                        string[] variablesList = null;
                        control.GetControlVariableList(file.ID, out variablesList);
                        if (variablesList != null && variablesList.Length > 0)
                            return variablesList[0];
                        else
                            return "N/A";
                    }
                }
            }

            return "N/A";
        }
    }
}



PS C:\Users\jgrewe\Development\JGrewe_code\GitRepo2\PDMAutoNoti\PDMFileVariables> dotnet run
Connected to vault: PDM Sandbox
File: 13010_IWO.xlsx, State: Released
PROJ_ID: Edit

Your GetVariableValue() function is actually just getting a list of variable names linked to the card controls. So, you’re just returning the first variable linked on the first control on the card with return variablesList[0]

To get the value of the variable, you’ll need to use IEdmEnumeratorVariable5.GetVar2()

var enumVariable = aFile.GetEnumeratorVariable();
enumVariable.GetVar2(variableName, configName, parentFolderId, out object variableValue);
var variableValueAsString = variableValue as string;
1 Like

example of data card format:
image

This got me to the right answer, for anyone interested, this code should print off all vraiables and values in your data card to the console.

using System;
using EPDM.Interop.epdm;
using System.Collections.Generic;

namespace PDMFileDetails
{
class Program
{
private static IEdmFolder5 ppoRetParentFolder;

    [STAThread]
    private static void Main(string[] args)
    {
        string vaultName = "";  // Replace with your actual vault name
        string filePath = @"";  // Adjust this to match your file path

        try
        {
            IEdmVault5 vault = new EdmVault5Class();
            vault.LoginAuto(vaultName, 0);

            if (vault.IsLoggedIn)
            {
                Console.WriteLine($"Connected to vault: {vaultName}");

                IEdmFile5 file = vault.GetFileFromPath(filePath, out ppoRetParentFolder);

                if (file != null)
                {
                    string stateName = file.CurrentState.Name;
                    Console.WriteLine($"File: {file.Name}, State: {stateName}");

                    // Retrieve and print all variable names and their values using GetVar2
                    PrintAllVariablesUsingGetVar2(file, ppoRetParentFolder.ID);

                    // Get and print the specific PROJ_ID value using GetVar2
                    string projID = GetVariableValue(file, "PROJ_ID", "@", ppoRetParentFolder.ID);
                    Console.WriteLine($"PROJ_ID: {projID}");
                }
                else
                {
                    Console.WriteLine($"File not found: {filePath}");
                }
            }
            else
            {
                Console.WriteLine("Failed to log in to the vault.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }

    public static void PrintAllVariablesUsingGetVar2(IEdmFile5 file, int folderID)
    {
        var variableNames = GetVariablesNames(file);
        var configurations = GetConfigurations(file);

        foreach (var variableName in variableNames)
        {
            foreach (var config in configurations)
            {
                var variableValue = GetVariableValue(file, variableName, config, folderID);
                Console.WriteLine($"Variable: {variableName}, Configuration: {config}, Value: {variableValue}");
            }
        }
    }

    static string[] GetVariablesNames(IEdmFile5 file)
    {
        var l = new List<string>();
        var vault = file.Vault;
        var card = file.GetNextFolder(file.GetFirstFolderPosition()).GetCard(System.IO.Path.GetExtension(file.Name).Replace(".", ""));
        var pos = card.GetFirstControlPosition();
        while (!pos.IsNull)
        {
            var control = card.GetNextControl(pos);
            var variableId = control.VariableID;

            if (variableId != 0)
            {
                var variable = vault.GetObject(EdmObjectType.EdmObject_Variable, variableId) as IEdmVariable5;

                if (!l.Contains(variable.Name))
                    l.Add(variable.Name);
            }
        }

        return l.ToArray();
    }

    static List<string> GetConfigurations(IEdmFile5 file)
    {
        var configurations = new List<string>();
        IEdmStrLst5 configList = file.GetConfigurations();
        IEdmPos5 pos = configList.GetHeadPosition();
        while (!pos.IsNull)
        {
            string configName = configList.GetNext(pos);
            configurations.Add(configName);
        }
        return configurations;
    }

    public static string GetVariableValue(IEdmFile5 file, string variableName, string config, int folderID)
    {
        if (file == null)
            throw new ArgumentNullException(nameof(file));

        if (string.IsNullOrEmpty(variableName))
            throw new ArgumentNullException(nameof(variableName));

        var enumVariable = file.GetEnumeratorVariable() as IEdmEnumeratorVariable10;

        if (enumVariable == null)
        {
            Console.WriteLine("Failed to get enumerator variable.");
            return "N/A";
        }

        try
        {
            enumVariable.GetVar2(variableName, config, folderID, out object variableValue);

            if (variableValue == null)
            {
                Console.WriteLine($"Variable {variableName} not found in configuration {config}.");
                return "N/A";
            }

            return variableValue.ToString() ?? "N/A";
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error retrieving variable {variableName} in configuration {config}: {ex.Message}");
            return "N/A";
        }
    }
}

}