Code Snippet: Custom Code Action

Applies to: SharePoint Server 2010

The following example shows the implementation of a custom code action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.Runtime;
using System.Windows.Forms;
using outlookPIA = Microsoft.Office.Interop.Outlook;

namespace ContosoCustomer
{
    public class CustomCodeAction
    {
        //Custom Code Action Method
        //The parameters array contains the entity instance for current item as the first parameter.
        public void NewCustomVisitTask(object[] parameters)
        {
            IEntityInstance iei = parameters[0] as IEntityInstance;
            try
            {
                //Get values from current EntityInstance.
                string CustomerFName = iei["FirstName"].ToString();
                string CustomerLName = iei["LastName"].ToString();

                //Create a new Outlook task Item through Outlook interop.
                //Trusted Outlook interop object is set in CustomRibbonManger.
                outlookPIA.TaskItem newCustomVisitTaskItem =
                    (outlookPIA.TaskItem)CustomRibbonManger.currentOutlookApplication.CreateItem(outlookPIA.OlItemType.olTaskItem);

                //set task properties.
                newCustomVisitTaskItem.Subject = "Custom Visit Task: " + CustomerFName + " " + CustomerLName;
                newCustomVisitTaskItem.StartDate = DateTime.Now;
                newCustomVisitTaskItem.DueDate = DateTime.Now.AddDays(7);
                newCustomVisitTaskItem.Save();

                //Display task item.
                newCustomVisitTaskItem.Display(false);
            }
            catch (Exception)
            {
                MessageBox.Show("Cannot find Entity Instance related to this item");
            }
        }
    }
}

See Also

Reference

IEntityInstance