Hide Table of Contents

Create a Task that Finds Approved Files Example (C#)

This example shows how to create a task add-in that notifies the logged-in user as to which files in the vault are in the Approved state.

// Preconditions:
// 1. Start Microsoft Visual Studio 2010.
// 2. Click File > New > Project > Visual C# > Class Library.
// 3. Select .NET Framework 2.0 in the dropdown at the top of the dialog.
// 4. Type TaskAddinExample in Name.
// 5. Click Browse and navigate to the folder where to create the project.
// 6. Click OK
// 7. Right-click the project name in the Solution Explorer and click Add Reference
// 8. In the Add Reference dialog:   
//    a. Add the SolidWorks Enterprise PDM type library as a reference (click COM in the
//       left-side panel, click PDMWorks Enterprise nnnn Type Library
//       and click Add).

//    b. Add System.Windows.Forms as a reference (click Assemblies > Framework in the

//       left-side panel, click System.Windows.Forms, and click Add).
//    c. Click Close.
// 9. Right-click the project name in the Solution Explorer and click Properties.
//10. In the Properties window:
//    a. Click Application > Assembly Information.
//    b. Select Make assembly COM-Visible.
//    c. Click OK.
//11. Copy the code below to Class1.cs.

//12. Click Build > Build Solution.

// Postconditions: 
//  1. Open the SolidWorks Enterprise PDM Administration tool, expand a vault_name node, 
//     and log in as Admin.

//  2. Ensure that Default Workflow with an Approved state exists 
//     under vault_name > Workflows.
//  3. Under vault_name, right-click Add-ins and click New Add-in.
//     a. Navigate to the
bin\Debug directory of your built project.
//     b. Click Interop.EdmLib.dll and TaskAddinExample.dll.
//     c. Click Open.
//     d. Click OK.
//  4. Click OK after reading the SolidWorks Enterprise PDM warning dialog.
//  5. In the taskbar notification area:
//     a. Click the SolidWorks Enterprise PDM icon.
//     b. Click Task Host Configuration.
//     c. Select vault_name in the File Vault dropdown.
//     d. Select Permit next to the add-in you installed in step 3.
//     e. Click OK.

//  6. In the Administration tool under the vault_name, right-click Tasks and click New Task.
//     a. Type Approved Files Task in the upper-left text box.
//     b. Select the C# Task Add-in from the Add-in dropdown.
//     c. When the attach debugger message box appears:
//        1. In the IDE, click Debug > Attach to Process > ConisioAdmin.exe.
//        2. Click Attach.
//        3. Click OK to close the message box.
//     d. Click Next.
//     e. On the Execution Method properties page, select the local computer.
//     f. Click Next.
//     g. On the Scheduling properties page, select This task is not scheduled.
//     h. Click Next.
//     i. On the Permissions property page, select Admin.
//     j. Click Next.
//     k. On the Success Notification property page:
//        1. Select Notify the user who launched the task.
//        2. In the Subject field, type Success.
//        3. In the Text field, type The task successfully completed at, type a space,
//           click >, and click Current Time.
//     l. Click OK to close the New Task dialog.
//  7. Expand vault_name > Tasks and double-click Task List.
//  8. Click Add Task
//  9. In the Add Task dialog:
//     a. Select Approved Files Task from the dropdown.
//     b. Click OK.
// 10. When the attach debugger message box appears:
//     a. In the IDE, click Debug > Attach to Process > TaskExecutor.exe.
//     b. Click Attach.
//     c. Click OK to close the message box.
// 11. The task appears in the Pending tasks list. Observe the task status.
// 12. When the task completes, the task moves to the Completed tasks
//     list. The logged-in user receives a notification with the list of approved files.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using EdmLib;
public class TaskAddinExample : IEdmAddIn5
    public void GetAddInInfo(ref EdmLib.EdmAddInInfo poInfo, EdmLib.IEdmVault5 poVault, EdmLib.IEdmCmdMgr5 poCmdMgr)
            poInfo.mbsAddInName = "C# Task Add-In";
            poInfo.mbsCompany = "Dassault Systemes";
            poInfo.mbsDescription = "Exercise demonstrating a task that lists files in the Approved state.";
            poInfo.mlAddInVersion = 1;
            //Minimum SolidWorks Enterprise PDM version
            //needed for C# Task Add-Ins is 10.0
            poInfo.mlRequiredVersionMajor = 10;
            poInfo.mlRequiredVersionMinor = 0;
            //Register this add-in as a task add-in
            //Register this add-in to be called when
            //selected as a task in the Administration tool
        catch (System.Runtime.InteropServices.COMException ex)
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") +  ex.Message);
        catch (Exception ex)
    public void OnCmd(ref EdmLib.EdmCmd poCmd, ref System.Array ppoData)
            switch (poCmd.meCmdType)
                case EdmCmdType.EdmCmd_TaskRun:
                    OnTaskRun(ref poCmd, ref ppoData);
                case EdmCmdType.EdmCmd_TaskSetup:
                    OnTaskSetup(ref poCmd, ref ppoData);
        catch (System.Runtime.InteropServices.COMException ex)
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
        catch (Exception ex)
    private void OnTaskRun(ref EdmCmd poCmd, ref System.Array ppoData)
        //Get the task instance interface
        IEdmTaskInstance inst = default(IEdmTaskInstance);
        inst = (IEdmTaskInstance)poCmd.mpoExtra;
            //Keep the Task List status up to date
            //Format a message that will be displayed
            //in the task list
            inst.SetProgressRange(10, 1, "Task is running.");
            List<EdmSelItem2> Items = new List<EdmSelItem2>();
            inst.SetProgressPos(4, "Task is searching.");
            IEdmSearch6 Search = (IEdmSearch6)((IEdmVault5)(poCmd.mpoVault)).CreateSearch();
            if (Search == null)
            Search.SetToken(EdmSearchToken.Edmstok_WorkflowName"Default Workflow");
            IEdmSearchResult5 SearchResult = Search.GetFirstResult();
            while ((SearchResult != null))
                EdmSelItem2 SelItem = new EdmSelItem2();
                SelItem.mlID = SearchResult.ID;
                SelItem.mlParentID = SearchResult.ParentFolderID;
                SelItem.meType = SearchResult.ObjectType;
                SelItem.mlVersion = SearchResult.Version;
                SearchResult = Search.GetNextResult();
            inst.SetProgressPos(7, "Task finished searching.");
            EdmSelItem2[] NotificationArray = new EdmSelItem2[Items.Count];
            string ProgresssMsg = null;
            if ((Items.Count > 0))
                ProgresssMsg = "Found " + Items.Count.ToString() + " files.";
                ProgresssMsg = ("No files found.");
            inst.SetProgressPos(10, ProgresssMsg);
            inst.SetStatus(EdmTaskStatus.EdmTaskStat_DoneOK, 0, "", NotificationArray, ProgresssMsg);
        catch (System.Runtime.InteropServices.COMException ex)
            inst.SetStatus(EdmTaskStatus.EdmTaskStat_DoneFailed, ex.ErrorCode, "The test task failed!");
        catch (Exception ex)
    private void OnTaskSetup(ref EdmCmd poCmd, ref System.Array ppoData)
            //Get the property interface used to
            //access the framework
            IEdmTaskProperties props = default(IEdmTaskProperties);
            props = (IEdmTaskProperties)poCmd.mpoExtra;
            //Set the property flag that says you want a
            //menu item for the user to launch the task
            //and a flag to support scheduling
            props.TaskFlags = (int)EdmTaskFlag.EdmTask_SupportsInitExec + (int)EdmTaskFlag.EdmTask_SupportsScheduling;
            //Set up the menu commands to launch this task
            EdmTaskMenuCmd[] cmds = new EdmTaskMenuCmd[1];
            cmds[0].mbsMenuString = "List Approved files task";
            cmds[0].mbsStatusBarHelp = "This command runs the task add-in to get the names of the files in the Approved state.";
            cmds[0].mlCmdID = 1;
            cmds[0].mlEdmMenuFlags = (int)EdmMenuFlags.EdmMenu_Nothing;
        catch (System.Runtime.InteropServices.COMException ex)
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
        catch (Exception ex)
    private void PauseToAttachProcess(string callbackType)
            //If the debugger isn't already attached to a
            if (!Debugger.IsAttached)
                //Launch the debug dialog
                //use a MsgBox dialog to pause execution
                //and allow the user time to attach it
                MessageBox.Show("Attach debugger to process \"" + Process.GetCurrentProcess().ProcessName + "\" for callback \"" + callbackType + "\" before clicking OK.");
        catch (Exception ex)

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Create a Task that Finds Approved Files Example (C#)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.