將 Service Manager 撰寫工具範例活動的狀態設定為已完成

重要

此版本的 Service Manager 已終止支援。 建議您升級至 Service Manager 2022

請參閱 Service Manager 中的下列範例活動,以設定要完成的活動狀態。

using System;  
using System.Linq;  
using System.Drawing;  
using System.Collections;  
using System.ComponentModel;  
using System.Workflow.Runtime;  
using System.Collections.Generic;  
using System.Workflow.Activities;  
using System.ComponentModel.Design;  
using Microsoft.EnterpriseManagement;  
using System.Workflow.ComponentModel;  
using System.Workflow.Activities.Rules;  
using System.Workflow.ComponentModel.Design;  
using Microsoft.EnterpriseManagement.Common;  
using System.Workflow.ComponentModel.Compiler;  
using System.Workflow.ComponentModel.Serialization;  
using Microsoft.EnterpriseManagement.Configuration;  
using Microsoft.EnterpriseManagement.Configuration.IO;  
using Microsoft.EnterpriseManagement.Workflow.Common;  

namespace Microsoft.ServiceManager.WorkflowAuthoring.ActivityLibrary  
{  
    // --------------------------------------------------------------------------------  
    /// <summary>  
    /// Activity to set an activity's status to complete  
    /// </summary>  
    // --------------------------------------------------------------------------------  
    [ToolboxItem(typeof(ActivityToolboxItem))]  
    [ActivityValidator(typeof(Validators.SetActivityStatusToCompletedValidator))]  
    [Designer(typeof(WorkflowActivityBaseDesigner))]  
    public sealed partial class SetActivityStatusToCompleted : WorkflowActivityBase  
    {  
        // --------------------------------------------------------------------------------  
        /// <summary>  
        /// Dependency Property for ActivityId property  
        /// </summary>  
        // --------------------------------------------------------------------------------  
        public static DependencyProperty ActivityIdProperty =   
            DependencyProperty.Register("ActivityId", typeof(String), typeof(SetActivityStatusToCompleted));  

        // --------------------------------------------------------------------------------  
        /// <summary>  
        /// Activity ID  
        /// </summary>  
        // --------------------------------------------------------------------------------  
        [Browsable(true)]  
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
        public string ActivityId  
        {  
            get  
            {  
                return (string)this.GetValue(ActivityIdProperty);  
            }  
            set  
            {  
                this.SetValue(ActivityIdProperty, value);  
            }  
        }  

        // --------------------------------------------------------------------------------  
        /// <summary>  
        /// The execute method will have the implementation to set the activity status to complete.  
        /// </summary>  
        // --------------------------------------------------------------------------------  
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)  
        {  
            try  
            {  
                // Initialize the current item if the activity contained within the For-Each loop  
                base.Execute(executionContext);  

                // Validate Parameters  
                if (String.IsNullOrEmpty(ActivityId))  
                {  
                    throw new ArgumentNullException("ActivityId");  
                }  

                string SMServer = "localhost";                  

                Guid TaskGuid = new Guid(ActivityId);  
                EnterpriseManagementGroup _mg = new EnterpriseManagementGroup(SMServer);  

                EnterpriseManagementObject Activity = _mg.EntityObjects.GetObject  
                    <EnterpriseManagementObject>(TaskGuid, ObjectQueryOptions.Default);  

                ManagementPack SystemMP = _mg.ManagementPacks.GetManagementPack(  
                    SystemManagementPack.System);  
                ManagementPack ActivityMP = _mg.ManagementPacks.GetManagementPack(  
                    Resources.ActivityManagementMP, SystemMP.KeyToken, SystemMP.Version);  

                ManagementPackClass activityClass = _mg.EntityTypes.GetClass(  
                    Resources.WorkItemActivityClass, ActivityMP);  

                ManagementPackProperty status = activityClass.PropertyCollection["Status"];  
                ManagementPackEnumeration Completed =   
                    _mg.EntityTypes.GetEnumeration("ActivityStatusEnum.Completed", ActivityMP);  

                Activity[status].Value = Completed;  
                Activity.Commit();  
            }  
            catch (ArgumentNullException argNullException)  
            {  
                // Log to Tracking Service  
                TrackData(argNullException.ToString());  

                throw;  
            }  
            catch (EnterpriseManagementException mgmtException)  
            {  
                TrackData(mgmtException.ToString());  
                throw;  
            }  

            return ActivityExecutionStatus.Closed;  
        }  
    }  
}