This page is now retired and kept for archival purposes. This programming guide has been published on MSDN at http://msdn.microsoft.com/library/jj860311.aspx

The information in this topic applies only to System Center 2012 SP1.

An action add-in is created by creating a class that inherits from the Microsoft.SystemCenter.VirtualMachineManager.UIAddIns.ActionAddInBase base class. Your class should then declare the System.Addin.AddinAttribute attribute, assigning it a unique name between all your other add-ins.

The ribbon button created by the console for your add-in uses the manifest to define the caption of the button. However, if you want to change the caption when your add-in is loaded, you can override the GetButtonLabelString method. This may be useful if you are trying to translate the caption into another language. If you want to change which objects, within the context identified by the manifest definition, will enable or disable your button, you can override the CheckIfEnabledFor method.

By overriding the PerformAction method, you can provide code that is invoked when the ribbon button is clicked.

To create a new action add-in

  1. Create a new class that inherits from Microsoft.SystemCenter.VirtualMachineManager.UIAddIns.ActionAddInBase .

  2. Declare the System.Addin.AddinAttribute attribute on the class and give it a unique name.

  3. Mark the scope of the class as public.

  4. Override the PerformAction method to define what your action will do when the ribbon button is clicked.

  5. Optionally override the CheckIfEnabledFor method to control which selected objects the add-in can operate with.

  6. Optionally override the GetButtonLabelString method to supply an alternative caption for the ribbon button.

Example

The following code example provides a skeleton that can be used when you are creating a new add-in:

Code:
[AddIn("Action Add-in 1")]
public class MyActionAddIn : ActionAddInBase
{
    public override bool CheckIfEnabledFor(IList<ContextObject> contextObjects)
    {
        if (contextObjects != null && contextObjects.Count > 0)
            return true;
        return false;
    }
    public override void PerformAction(IList<ContextObject> contextObjects)
    {
    }
}

The following code example demonstrates an add-in that enables or disables the ribbon button based on the state of the HostContext object selected in the console. It also calls out to PowerShell when the user clicks on the ribbon button:

Code:
[AddIn("Host Action Button")]
public class HostActionAddIn : ActionAddInBase
{
    public override bool CheckIfEnabledFor(IList<ContextObject> contextObjects)
    {
        if (contextObjects != null && contextObjects.Count > 0)
        {
            foreach (var host in contextObjects.OfType<HostContext>())
            {
                // Action only applies for running hosts
                if (host.ComputerState != ComputerState.Responding)
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
    public override void PerformAction(IList<ContextObject> contextObjects)
    {
        if (contextObjects != null)
        {
            // Get a list of VMs owned by the first host in the object list
            HostContext host = contextObjects.OfType<HostContext>().FirstOrDefault();
            if (host != null)
            {
                string getScript =
                    string.Format(
                        "$h = Get-SCVMHost -ID {0}; Get-SCVirtualMachine -VMHost $h",
                        host.ID);
                this.PowerShellContext.ExecuteScript<VM>(
                    getScript,
                    (vms, error) =>
                    {
                        if (error == null)
                        {
                            System.Windows.MessageBox.Show(
                                string.Format(
                                    "There are {0} VMs on host {1}.",
                                    vms.Count(),
                                    host.Name));
                        }
                        else
                        {
                            System.Windows.MessageBox.Show(
                                "An error occured: " + error.Problem);
                        }
                    });
            }
        }
    }
}

Compiling the Code

Namespaces

System.Linq

System.AddIn

System.Collections.Generic

Microsoft.SystemCenter.VirtualMachineManager.UIAddIns

Microsoft.SystemCenter.VirtualMachineManager.UIAddIns.ContextTypes

Microsoft.VirtualManager.Remoting

Microsoft.SystemCenter.VirtualMachineManager

Assemblies

System.AddIn

PresentationFramework

mscorlib

System.Core

Microsoft.SystemCenter.VirtualMachineManager

Microsoft.SystemCenter.VirtualMachineManager.UIAddIns

Microsoft.SystemCenter.VirtualMachineManager.UIAddins.ContextTypes

Errors

Remoting

See Also