Custom Action Help : Custom Action : ICustomActionEx Interface : Example

Example
The following is a simple implementation that demonstrates how to retrieve SPListItem object(s) from the passed in parameter, process the parameters, and how to notify the web parts about the outcome of the custom action.
Below is an example of a custom action class. This class verifies that if this item has high priority, it must be assigned to somebody. Otherwise, it will cancel the operation.
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Microsoft.SharePoint;
using WA.Core;
 
namespace MyCompany.DevStudio
{
/// <summary>
/// A sample custom action that you can use to validate a Task in the Tasks list.
///
/// In this case, the sample custom action ensures that a high priority task must be assigned to somebody.
/// If not, the sample custom action will cancel the action and generates an error message.
/// </summary>
    public class SampleCustomActionEx : ICustomActionEx
    {
/// <summary>
/// The Perform method is called by the webpart when the action item (such
/// as toolbar button or context menu item) is clicked.
/// </summary>
/// <param name="context">The CustomActionContext object that contains the HTTP Request and HTTP Context.
/// If the custom action is invoked from an SI Web Part, it will also contain a collection of DataRows
/// where the action is performed. If the custom action is invoked from a non-SI Web Part, it will contain
/// a collection of SPListItem object.</param>
/// <param name="evt">The event argument that can be used to cancel the action and
/// to carry the error message back to the Web Part.</param>
/// <returns></returns>
        public ActionResult Perform(CustomActionContext context, CustomActionCancelEventArgs evt)
        {
            ActionResult actionResult = new ActionResult();
            if (context.Items == null || context.Items.Count == 0)
            {
                evt.Cancel = true;
// you may construct a simple message for popup message or html text if message is shown as inline
                evt.Message = "There is no item to validate.";
                return actionResult;
            }
// This sample custom action is meant to be used by a qListView or qListForm
// that points to a Task list. Therefore, the items contains SPListItem objects.
            SPListItem item = null;
            IEnumerator enumerator = context.Items.GetEnumerator();
            while (enumerator.MoveNext())
            {
                item = enumerator.Current as SPListItem;
                if (item != null)
                {
                    if (item["Priority"] != null)
                   {
                        string priority = item["Priority"].ToString();
                        if ("(1) High".Equals(priority))
                       {
                            if (item["Assigned To"] == null ||
                            string.IsNullOrEmpty(item["Assigned To"].ToString()))
                           {
                                evt.Cancel = true;
                                evt.Message = "A high priority task must be assigned to a user";
                            }
                        }
                    }
                }
            }
/* The following code shows you how you can store the result from this action to be used by another action
* in the chain of actions of the ActionItem. The lines are commented out because they are not really used
* in this action. It is for illustration purposes only.
*
* You have two ways to store the data. First, you can store the data as RawData in the actionResult. Here is an example:
            actionResult.RawData = someData;
* You can also store the data as key-value pairs in the OutParameters of the actionResult. Here is an example:
*
            actionResult.OutParameters["Name"] = someData;
*/
/* The following code shows you how you can use the result from another action. The lines are
* commented out because they are not really used in this action. It is for illustration purpose only.
*
* You can access the RawData that comes from another action the following name.
* ActionName - this is the name of the action that stores the result.
* RawData - use this specifier to get the RawData
*
            object rawData = context.GetActionResult("ActionName.RawData");
 
* You can also access the key-value pairs in the OutParameters by using the following specifier:
* ActionName - this is the name of the action that stores the result.
* OutParameter - use this specifier to get to the OutParameters key-value pairs
* Name - the key for the value in the OutParameters key-value pairs
            object val = context.GetActionResult("ActionName.OutParameter.Name");
*/
            return actionResult;
        }
    }
}
Here is another example of a custom action class. This custom action class can be used to initialize a qListForm.
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebPartPages;
using System.IO;
using System.Net.Mail;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Threading;
using WA.Core;
 
namespace MyCompany.DevStudio
{
    /// <summary>
    /// A sample custom action that you can use to validate a Task in the Tasks list.
    ///
    /// In this case, the sample custom action ensures that a high priority task must be assigned to somebody.
    /// If not, the sample custom action will cancel the action and generates an error message.
    /// </summary>
    public class InitializeFormAction : ICustomActionEx
    {
        /// <summary>
        /// The Perform method is called by the webpart when the fields in the list form are initialized.
        /// </summary>
        /// <param name="context">The CustomActionContext object that contains the HTTP Request and HTTP Context.
        /// If the custom action is invoked from an qSIListForm, it will also contain a collection of Hashtable
        /// where the action is performed. If the custom action is invoked from a qListForm, it will contain
        /// a collection of SPListItem object.</param>
        /// <param name="evt">The event argument that can be used to cancel the action and
        /// to carry the error message back to the Web Part.</param>
        /// <returns></returns>
        public ActionResult Perform(CustomActionContext context, CustomActionCancelEventArgs evt)
        {
            ActionResult actionResult = new ActionResult();
 
            if (context.Items == null || context.Items.Count == 0)
            {
                return actionResult;
            }
 
            // This sample custom action is meant to be used by a qListForm
            // that points to a Task list. Therefore, the items contains SPListItem objects.
            SPListItem item = null;
            IEnumerator enumerator = context.Items.GetEnumerator();
            while (enumerator.MoveNext())
            {
                item = enumerator.Current as SPListItem;
                if (item != null)
                {
                    // Initialize the title with a value
                    item["Title"] = "New Task";
                }
            }
            return actionResult;
        }
    }