IEnumerable<TaskProjectItem>taskItems= Project.Current.GetItems<TaskProjectItem>();foreach(var item in taskItems){// do something}
Open a Task File - .esriTasks file
// Open a task filetry{// TODO - substitute your own .esriTasks file to be openedstringtaskFile=@"c:\Tasks\Get Started.esriTasks";//At 2.x -//System.Guid guid = await TaskAssistantModule.OpenTaskAsync(taskFile);
System.Guid guid;if(TaskAssistantFactory.Instance.CanOpenTaskFile(taskFile))guid=await TaskAssistantFactory.Instance.OpenTaskFileAsync(taskFile);// TODO - retain the guid returned for use with CloseTaskItemAsync}catch(OpenTaskExceptione){// exception thrown if task file doesn't exist or has incorrect format
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(e.Message);}
Open a Project Task Item
// get the first project task itemvartaskItem= Project.Current.GetItems<TaskProjectItem>().FirstOrDefault();// if there isn't a project task item, returnif(taskItem==null)return;try{// Open it//At 2.x -//System.Guid guid = await TaskAssistantModule.OpenTaskItemAsync(taskItem.TaskItemGuid);varguid=await TaskAssistantFactory.Instance.OpenTaskItemAsync(taskItem.TaskItemGuid);// TODO - retain the guid returned for use with CloseTaskItemAsync}catch(OpenTaskExceptione){
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(e.Message);}
Close a Task Item
// find the first project task item which is openvartaskItem= Project.Current.GetItems<TaskProjectItem>().FirstOrDefault(t => t.IsOpen ==true);// if there isn't a project task item, returnif(taskItem==null)return;if(taskItem.IsOpen){// close it// NOTE : The task item will also be removed from the project//At 2.x -//TaskAssistantModule.CloseTaskAsync(taskItem.TaskItemGuid);
TaskAssistantFactory.Instance.CloseTaskItemAsync(taskItem.TaskItemGuid);}
Export a Task Item
// get the first project task itemvartaskItem= Project.Current.GetItems<TaskProjectItem>().FirstOrDefault();// if there isn't a project task item, returnif(taskItem==null)return;try{// export the task item to the c:\Temp folderstringexportFolder=@"c:\temp";//At 2.x -//string fileName = await TaskAssistantModule.ExportTaskAsync(taskItem.TaskItemGuid, exportFolder);stringfileName=await TaskAssistantFactory.Instance.ExportTaskItemAsync(taskItem.TaskItemGuid, exportFolder);
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Task saved to "+fileName);}catch(ExportTaskExceptione){
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Error saving task "+ e.Message);}
Get Task Information - from a TaskProjectItem
vartaskItem= Project.Current.GetItems<TaskProjectItem>().FirstOrDefault();// if there isn't a project task item, returnif(taskItem==null)return;stringmessage=await QueuedTask.Run(async()=>{boolisOpen= taskItem.IsOpen;GuidtaskGuid= taskItem.TaskItemGuid;stringmsg="";try{TaskItemInfotaskItemInfo=await taskItem.GetTaskItemInfoAsync();msg="Name : "+ taskItemInfo.Name;msg+="\r\n"+"Description : "+ taskItemInfo.Description;msg+="\r\n"+"Guid : "+ taskItemInfo.Guid.ToString("B");msg+="\r\n"+"Task Count : "+ taskItemInfo.GetTasks().Count();// iterate the tasks in the task itemIEnumerable<TaskInfo>taskInfos= taskItemInfo.GetTasks();foreach(TaskInfo taskInfo in taskInfos){stringname= taskInfo.Name;Guidguid= taskInfo.Guid;// do something }}catch(OpenTaskExceptione){// exception thrown if task file doesn't exist or has incorrect formatmsg= e.Message;}catch(TaskFileVersionExceptione){// exception thrown if task file does not support returning task informationmsg= e.Message;}returnmsg;});
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,"Task Information");
Get Task Information - from an .esriTasks file
// TODO - substitute your own .esriTasks filestringtaskFile=@"c:\Tasks\Get Started.esriTasks";stringmessage=await QueuedTask.Run(async()=>{stringmsg="";try{// retrieve the task item information//At 2.x -//TaskItemInfo taskItemInfo = await TaskAssistantModule.GetTaskItemInfoAsync(taskFile);TaskItemInfotaskItemInfo=await TaskAssistantFactory.Instance.GetTaskItemInfoAsync(taskFile);msg="Name : "+ taskItemInfo.Name;msg+="\r\n"+"Description : "+ taskItemInfo.Description;msg+="\r\n"+"Guid : "+ taskItemInfo.Guid.ToString("B");msg+="\r\n"+"Task Count : "+ taskItemInfo.GetTasks().Count();}catch(OpenTaskExceptione){// exception thrown if task file doesn't exist or has incorrect formatmsg= e.Message;}catch(TaskFileVersionExceptione){// exception thrown if task file does not support returning task informationmsg= e.Message;}returnmsg;});
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(message,"Task Information");
Open a specific Task in a Task File - .esriTasks file
// TODO - substitute your own .esriTasks file to be openedstringtaskFile=@"c:\Tasks\Get Started.esriTasks";await QueuedTask.Run(async()=>{try{// retrieve the task item information//At 2.x -//TaskItemInfo taskItemInfo = await TaskAssistantModule.GetTaskItemInfoAsync(taskFile);vartaskItemInfo=await TaskAssistantFactory.Instance.GetTaskItemInfoAsync(taskFile);// find the first taskTaskInfotaskInfo= taskItemInfo.GetTasks().FirstOrDefault();Guidguid= Guid.Empty;if(taskInfo!=null){// if a task exists, open it//At 2.x -//guid = await TaskAssistantModule.OpenTaskAsync(taskFile, taskInfo.Guid);guid=await TaskAssistantFactory.Instance.OpenTaskFileAsync(taskFile, taskInfo.Guid);}else{// else just open the task item//At 2.x -//guid = await TaskAssistantModule.OpenTaskAsync(taskFile);guid=await TaskAssistantFactory.Instance.OpenTaskFileAsync(taskFile);}// TODO - retain the guid returned for use with CloseTaskItemAsync }catch(OpenTaskExceptione){// exception thrown if task file doesn't exist or has incorrect format ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(e.Message);}catch(TaskFileVersionExceptione){// exception thrown if task file does not support returning task information ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(e.Message);}});
Subscribe to Task Events
publicvoidTaskEvents(){
TaskStartedEvent.Subscribe(OnTaskStarted);
TaskEndedEvent.Subscribe(OnTaskCompletedOrCancelled);}privatevoidOnTaskStarted(TaskStartedEventArgsargs){stringuserName= args.UserID;// ArcGIS Online signed in userName. If not signed in to ArcGIS Online then returns the name of the user logged in to the Windows OS.stringprojectName= args.ProjectName;GuidtaskItemGuid= args.TaskItemGuid;stringtaskItemName= args.TaskItemName;stringtaskItemVersion= args.TaskItemVersion;GuidtaskGuid= args.TaskGuid;stringtaskName= args.TaskName;DateTimestartTime= args.StartTime;}privatevoidOnTaskCompletedOrCancelled(TaskEndedEventArgsargs){stringuserName= args.UserID;// ArcGIS Online signed in userName. If not signed in to ArcGIS Online then returns the name of the user logged in to the Windows OS.stringprojectName= args.ProjectName;GuidtaskItemGuid= args.TaskItemGuid;stringtaskItemName= args.TaskItemName;stringtaskItemVersion= args.TaskItemVersion;GuidtaskGuid= args.TaskGuid;stringtaskName= args.TaskName;DateTimestartTime= args.StartTime;DateTimeendTime= args.EndTime;doubleduration= args.Duration;boolcompleted= args.Completed;// completed or cancelled}