Item Event Receiver

The framework introduces an extended item event receiver mechanism that allows you to split the code of your traditional event receiver into separated tasks. The core of this mechanism is the fact that it allows you to tell the system which part will be executed when a certain field of the item changes.

To make this clearer to you, let's see an example.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Sapiens.at.SharePoint.Receiver;
using Sapiens.at.FrameworkSample.Entities;
using Sapiens.at.SharePoint.Repositories;

namespace Sapiens.at.FrameworkSample.Receiver.EmployeeReceiver
{
    public class EmployeeReceiver : BaseItemReceiver // inherit BaseItemReceiver

    {
        public EmployeeReceiver() // create a parameterless constructor to register your events
        {
            // register your event
            AddItemEventConfig(new ItemEventConfig
            {
                EventTypes = new SPEventReceiverType[] { SPEventReceiverType.ItemAdded, SPEventReceiverType.ItemUpdating },
                OnChangedFields = new string[] { Employee.LOOKUP_DEPARTMENT },
                Callback = OnDepartmentChange
            });
        }

        private void OnDepartmentChange(ItemEventArgs args)
        {
            var employee = (Employee)args.Entity;

            // Decrease the number of employees of the previous department
            if (args.ChangedValues.ContainsKey(Employee.LOOKUP_DEPARTMENT) &&
                !string.IsNullOrEmpty(Convert.ToString(args.ChangedValues[Employee.LOOKUP_DEPARTMENT])))
            {
                var repository = new ListRepository(Department.ListUrl, args.Web);
                var oldDepartment = repository.GetEntity<Department>(new SPFieldLookupValue(Convert.ToString(args.ChangedValues[Employee.LOOKUP_DEPARTMENT])).LookupId);
                oldDepartment.DecreaseEmployeeCount();
            }

            // Increase the number of employees of the new department
            employee.Department.IncreaseEmployeeCount();
        }
    }
}

Now register the class EmployeeReceiver as an event receiver as you would do it with your standard event receiver. Internally, the BaseItemReceiver handles the event and calls all registered methods that match the current event properties.

With the above example you registered the method OnDepartmentChange as an event receiver that gets called when the event type is ItemAdded or ItemUpdating and the Department property is about to change.

The LimitToContentTypeAttribute

The LimitToContentTypeAttribute helps you with event receiver that are bound to a list, but you want its callbacks only to fire when the context listitem is of a certain content type. Just mark the receiver class with the attribute and it will be ignored when the listitem does not match.
[LimitToContentType("0x0100xxx", "0x0102")] // processes the event receiver only if the current listitem matches these content types
public class MyReceiver : BaseItemReceiver // inherit BaseItemReceiver
{
    // content of the event receiver
}



Last edited Jul 6, 2011 at 10:05 AM by florianwachter, version 7

Comments

No comments yet.