Get file previous state using PDM API

Hi All,

Please let me know the way to get file’s previous state in Solidworks PDM using API. I’m using state change events.
I want to know, what is the previous state of the file?

  /// <summary>
  /// Gets the name of the previous file's state.
  /// </summary>
  /// <param name="file">File</param>
  /// <returns></returns>
  /// <exception cref="System.ArgumentNullException">File is null.</exception>
  /// <exception cref="System.Runtime.InteropServices.COMException">
  /// File has state changes history.
  /// or
  /// File has no history.
  /// </exception>
        public static string GetPreviousState(this IEdmFile5 file)
        {

            var stateName = string.Empty;

            if (file == null)
                throw new ArgumentNullException("file");

            var vault = file.Vault as IEdmVault7;

            var history = vault.CreateUtility(EdmUtility.EdmUtil_History) as IEdmHistory;

            EdmHistoryItem[] historyitems = null;
            history.AddFile(file.ID);
            history.GetHistory(ref historyitems, (int)EdmHistoryType.Edmhist_FileState);

            if (history == null)
                throw new COMException("File has state changes history");

            if (historyitems.Length == 0)
                throw new COMException("File has no history.");

          

            stateName = historyitems[0].moData.mbsStrData1;

            var workflow = vault.GetObject(EdmObjectType.EdmObject_Workflow, (file.CurrentState as IEdmState6).WorkflowID) as IEdmWorkflow6;

            return stateName.Replace(workflow.Name,"").Trim();

        }

3 Likes