Share via


How to Create Task Handlers

Applies To: System Center 2012 - Service Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Task handlers enable custom code to be run when a task is executed by the Service Manager console. Task handlers can reside in any assembly that is loaded by the console. If an assembly is not signed, the system warns the end user that it is about to execute a task in an unsigned assembly. The warning also provides the option to discontinue. Task handlers use two assemblies provided by Service Manager. The first assembly, Microsoft.EnterpriseManagement.UI.SdkDataAccess.dll, contains the Microsoft.EnterpriseManagement.UI.SdkDataAccess.ConsoleCommand class. To create a task handler you must inherit from this class. The second assembly used by the task handler is Microsoft.EnterpriseManagement.UI.Foundation.dll. This assembly provides some classes used by the task handler method that you must override in order to implement your custom code.

Assemblies that provide custom task handlers must target the Microsoft .NET 3.5 framework. An assembly will only be recognized by Service Manager console after it has been copied to the Service Manager install directory.

To create a console task handler

  1. Create a project that compiles to a DLL and targets the Microsoft .NET 3.5 framework.

  2. Add reference to the Microsoft.EnterpriseManagement.UI.SdkDataAccess.dll and Microsoft.EnterpriseManagement.UI.Foundation.dll libraries. These are located in the Service Manager console install directory.

  3. Create a new class that inherits from Microsoft.EnterpriseManagement.UI.SdkDataAccess.ConsoleCommand.

  4. Override the ExecuteCommand method.

  5. Add any custom handler code to the ExecuteCommand method.

  6. Compile the project and copy the output DLL to the Service Manager install directory.

Example

The following example creates a custom task handler. It shows how to connect to the management group and to the object that was selected when end user clicked the task. It also changes a property on that object and commits the changes back to Service Manager.

namespace RePackaging.Handlers
{
    public class AcceptRequest : Microsoft.EnterpriseManagement.UI.SdkDataAccess.ConsoleCommand
    {
        public override void ExecuteCommand(IList<Microsoft.EnterpriseManagement.ConsoleFramework.NavigationModelNodeBase> nodes, Microsoft.EnterpriseManagement.ConsoleFramework.NavigationModelNodeTask task, ICollection<string> parameters)
        {
            foreach (var node in nodes)
            {
                EnterpriseManagementGroup mg = node["ManagementGroup"] as EnterpriseManagementGroup;
                EnterpriseManagementObject emo = node["$EMOInstance$"] as EnterpriseManagementObject;

                if (mg != null && emo != null)
                {
                    ManagementPackClass mpClass = mg.GetManagementPack("RePackaging.Library", null, new Version()).GetClass("RePackaging.Request");

                    if (emo.IsInstanceOf(mpClass))
                    {
                        emo[mpClass, "IsAccepted"].Value = true;
                        emo.Commit();
                    }
                }
            }
        }

    }

}

Compiling the Code

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Configuration

Microsoft.EnterpriseManagement.Common

Microsoft.EnterpriseManagement.ConsoleFramework

Microsoft.EnterpriseManagement.UI.SdkDataAcces

System

System.Collections.Generic

Assemblies

Microsoft.EnterpriseManagement.Core

Microsoft.EnterpriseManagement.UI.SdkDataAccess

Microsoft.EnterpriseManagement.UI.Foundation

mscorlib

See Also

Tasks

How to Create a Folder Task

Other Resources

Scenario: Working with Console Tasks