Skip to content

ProSnippets WorkflowManager

UmaHarano edited this page Nov 6, 2024 · 6 revisions
Language:              C#  
Subject:               WorkflowManager  
Contributor:           ArcGIS Pro SDK Team <arcgisprosdk@esri.com>  
Organization:          esri, http://www.esri.com  
Date:                  10/22/2024  
ArcGIS Pro:            3.4  
Visual Studio:         2022  
.NET Target Framework: .Net 8  

How to determine if there is an active Workflow Manager connection

// determine if there is an active Workflow Manager connection
var isConnected = WorkflowClientModule.IsConnected;

How to get the Workflow Manager item Id

// Get the Workflow Manager item Id
var itemId = WorkflowClientModule.ItemId;

How to get the Workflow Manager server url

// Get the Workflow Manager server url
var serverUrl = WorkflowClientModule.ServerUrl;

How to get the job Id associated with the active map view

// Get the job Id associated with the active map view
var jobManager = WorkflowClientModule.JobsManager;
var jobId = jobManager.GetJobId();

How to get the job Id associated with a map

// Get the job Id associated with a map
mapUri = "myMapUri"; // Get a reference to a map using the ArcGIS.Desktop.Mapping API (active view, project item, etc.)
var jobManager = WorkflowClientModule.JobsManager;
var jobId = jobManager.GetJobId(mapUri);

How to get the job Id associated with a running OpenProProjectItems step

// Get the job Id associated with a running OpenProItems step for a Pro Add-In module

// In the Add-In Module class, override the ExecuteCommandArgs(string id) method and return a Func<Object[], Task> object like the sample below
// Refer to the Workflow Manager ProConcepts Sample Code link for an example

//protected override Func<Object[], Task> ExecuteCommandArgs(string id)
//{
//    return func1;
//}

Func<Object[], Task> func1 = (object[] args) => QueuedTask.Run(() =>
{
    try
    {
        // Get the jobId property from the OpenProProjectItemsStep arguments and store it.
        OpenProProjectItemsStepCommandArgs stepArgs = (OpenProProjectItemsStepCommandArgs)args[0];
        var jobId = stepArgs.JobId;
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show($"Got job id from ProMappingStep args: {jobId}", "Project Info");

        // Run the command specified by the id passed into ExecuteCommandArgs
        IPlugInWrapper wrapper = FrameworkApplication.GetPlugInWrapper(id);
        var command = wrapper as ICommand;
        if (command != null && command.CanExecute(null))
            command.Execute(null);
    }
    catch (System.Exception e)
    {
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show($"ERROR: {e}", "Error running command");
    }
});

How to get a job

// GetJob returns an existing job
try
{
  var jobManager = WorkflowClientModule.JobsManager;
  var job = jobManager.GetJob(jobId);
  // Do something with the job
}
catch (NotConnectedException)
{
  // Not connected to Workflow Manager server, do some error handling
}

Search for jobs using a detailed query

var search = new SearchQuery()
{
  // Search for all open high priority jobs assigned to users
  Q = "closed=0 AND assignedType='User' AND priority='High'",
  Fields = new List<string> { "jobId", "jobName", "assignedTo", "dueDate" },
  // Sort by job assignment in ascending order and due date in descending order
  SortFields = new List<SortField> 
  { 
    new SortField() { FieldName = "assignedTo", SortOrder = ArcGIS.Desktop.Workflow.Client.Models.SortOrder.Asc },
    new SortField() { FieldName = "dueDate", SortOrder = ArcGIS.Desktop.Workflow.Client.Models.SortOrder.Desc }
  }
};
var jobManager = WorkflowClientModule.JobsManager;
var searchResults = jobManager.SearchJobs(search);
var fields = searchResults.Fields;
var results = searchResults.Results;

Search for jobs using a detailed query with an arcade expression

var search = new SearchQuery()
{
  // Search for jobs assigned to the current user using the arcade expression '$currentUser'
  Q = "\"assignedType='User' AND closed=0 AND assignedTo='\" + $currentUser + \"' \"",
  Fields = new List<string> { "jobId", "jobName", "assignedTo", "dueDate"},
  // Sort by job name in ascending order
  SortFields = new List<SortField> { new SortField() { FieldName = "jobName", SortOrder = ArcGIS.Desktop.Workflow.Client.Models.SortOrder.Asc }}
};
var jobManager = WorkflowClientModule.JobsManager;
var searchResults = jobManager.SearchJobs(search);
var fields = searchResults.Fields;
var results = searchResults.Results;

Search for jobs using a simple string

var search = new SearchQuery() { Search = "My Search String" };
var jobManager = WorkflowClientModule.JobsManager;
var searchResults = jobManager.SearchJobs(search);
var fields = searchResults.Fields;
var results = searchResults.Results;

Get statistics for jobs

var query = new JobStatisticsQuery()
{
  // Search for open jobs assigned to users
  Q = "\"assignedType='User' AND closed=0 \""
};
var jobManager = WorkflowClientModule.JobsManager;
var results = jobManager.CalculateJobStatistics(query);
var totalJobs = results.Total;

How to run steps on a job

var jobManager = WorkflowClientModule.JobsManager;
jobManager.RunSteps(jobId);

How to run specific steps on a job

var jobManager = WorkflowClientModule.JobsManager;
// Specify specific current steps in a job to run
stepIds = new List<string> { "step12345", "step67890" };
jobManager.RunSteps(jobId, stepIds);

How to stop running steps on a job

var jobManager = WorkflowClientModule.JobsManager;
// Get the job Id associated with the active map view
var jobId = jobManager.GetJobId();
// Stop the current steps in the job with the given id.
jobManager.StopSteps(jobId);

How to stop specific running steps on a job

var jobManager = WorkflowClientModule.JobsManager;
// Get the job Id associated with the active map view
var jobId = jobManager.GetJobId();
// Specify specific running steps in a job to stop
stepIds = new List<string> { "step12345", "step67890" };
jobManager.StopSteps(jobId, stepIds);

How to finish steps on a job

var jobManager = WorkflowClientModule.JobsManager;
// Finish the current steps in the job with the given id.
jobManager.FinishSteps(jobId);

How to finish specific steps on a job

var jobManager = WorkflowClientModule.JobsManager;
stepIds = new List<string> { "step12345", "step67890" };
jobManager.FinishSteps(jobId, stepIds);

How to subscribe to a workflow connection changed event.

var subscriptionToken = WorkflowConnectionChangedEvent.Subscribe(e =>
{
  // The connection has changed - Get the user's sign in status
  var isUserSignedIn = e.IsSignedIn;
  return Task.CompletedTask;
});

How to unsubscribe from a workflow connection changed event.

var subscriptionToken = WorkflowConnectionChangedEvent.Subscribe(e =>
{
  var isUserSignedIn = e.IsSignedIn;
  return Task.CompletedTask;
});

WorkflowConnectionChangedEvent.Unsubscribe(subscriptionToken);
Clone this wiki locally