Partilhar via


Event Registrations

Applies to: SharePoint Foundation 2010

Register event handlers at the site or site collection level that you will use to implement filtering to respond to events.

The Receivers element specifies an event handler for list item events.

Elements

  Receivers

    Receiver

      Assembly

      Class

      Data

      Filter Element (Event)

      Name

      SequenceNumber

      SolutionId Element (Event)

      Synchronization Element (Event)

      Type

Example

To register an event handler for list events, create a folder in \Template\Features to contain a Feature.xml file that specifies the scope and ID of the Feature, and an elements manifest file that the former file references.

The Feature.xml file that registers an event handler might look like the following.

<Feature 
  Scope="Web" 
  Title="Simple Updating Item Event Handler Registration" 
  Id="A6B8687A-3200-4b01-AD76-09E8D163FB9A" 
  xmlns="https://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

The elements manifest file registers the event handler assembly and associates it with a list type, which the following example specifies to be announcement lists (104).

<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <Receivers 
    ListTemplateId="104">
    <Receiver>
      <Name>SimpleUpdateEvent</Name>
      <Type>ItemUpdating</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>SimpleUpdateEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10b23036c9b36d6d</Assembly>
      <Class>MS.Samples.SimpleItemUpdateHandler</Class>
      <Data></Data>
    </Receiver>
  </Receivers>
</Elements>

The .cs file for the event handler can use the SharePoint Foundation object model to respond to events. For information about using the object model to create custom event handlers, see Events in SharePoint Foundation 2010.

The following example defines the content of an error message to display when users attempt to modify items in a list.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MS.Samples
{
    public class SimpleItemUpdateHandler : SPItemEventReceiver
    {
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            properties.Cancel = true;
            properties.ErrorMessage = "Updating data is not supported.";
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint

Namespace MS.Samples
    Public Class SimpleItemUpdateHandler
        Inherits SPItemEventReceiver
        Public Overrides Sub ItemUpdating(ByVal properties As SPItemEventProperties)
            properties.Status = SPEventReceiverStatus.CancelWithError
            properties.ErrorMessage = "Updating data is not supported."
        End Sub
    End Class
End Namespace

See Also

Tasks

How to: Create an Event Handler Feature

Other Resources

Events in SharePoint Foundation 2010