How to Create an Action Add-in

 

Applies To: System Center 2012 SP1

You can build an action add-in by creating a class that inherits from the ActionAddInBase base class. The class that you create should then declare the T:System.Addin.AddinAttribute attribute and it should assign a unique name to this attribute and between all your other add-ins.

The console will create a ribbon button for each add-in. This ribbon button 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 try to translate the caption into another language. If you want to change which objects, within the context that is identified by the manifest definition, will enable or disable your button, you can override the CheckIfEnabledFor method.

If you choose to override 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 ActionAddInBase.

  2. Declare the T: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 create a new add-in:

  
[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)  
    {  
  
    }  
}  
  

Example

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

  
[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

Simple Add-ins Overview
How to Use Context Objects
How to Define a Custom Action Add-in in the Manifest