Escolar Documentos
Profissional Documentos
Cultura Documentos
E vent Receivers (formerly known as event handlers) are yet another tool in the arsenal
for working with and extending SharePoint. Event Receivers are code assemblies that
you can attach to a given list or library type and that allow you to respond to a given
event, such as an item being changed. Receivers provide a way to extend SharePoint both
internally and externally, because receivers are not bound to SharePoint. They are started and
run by SharePoint and have complete access to the SharePoint object model (and subsequently
any lists, libraries, sites, and so on, within it), but as a .NET assembly, they can call web
services, access databases, and they can even send e-mail.
Event Receivers apply to all items within SharePoint including lists and libraries. When
an event occurs—such as a file move, a document upload, or similar—the receiver is notified
about the event that occurred, as well as about what the item (and columns) looked like
before and after the event. This also includes some trapping; for example, a file delete can
be prevented before it occurs.
The mechanics of an Event Receiver are the same as for Features or web parts; Feature
definition files are used to define the assembly and also to define the type of list/library (and
the events in that list) that the receiver will listen for. Multiple receivers can also work on the
same list type (like a workflow). Like a Feature, they can be enabled or disabled by site.
What Receivers Are For
Event Receivers give you the ability to react to a list or library event within SharePoint,
which includes adding, updating, and deleting any item. For example, a Receiver might
trigger when a Task Item is created and in turn, create a Calendar Item on another site.
One receiver can react to any number of events.
You might be asking yourself why you would use Event Receivers when you have alerts
and built-in workflows. The answer is simple: there are times when a workflow is too much
or not enough; for example, if you want to send an e-mail message to a third party (like an
auditor) when a certain document type is published. If you’re not a member of the site, you
can’t use alerts. You could create a very simple workflow, but you would have to add it
everywhere. An Event Receiver is tied to a list type, can run on all sites, and can easily
handle such one-off or otherwise unique activities.
Event Receivers are really good for handling integrity, one-off situations, and for extending
SharePoint beyond what workflows can do and into other systems like Microsoft BizTalk
and CRM. On the integrity side, Event Receivers can be used to ensure that items are
correctly entered or that certain conditions are met before permitting a change. Event
Receivers provide before and after methods, which permit you to catch what’s changing
before it changes. To extend SharePoint, Event Receivers can call web services, update
databases, provide input to backend systems, and so on
While it’s tempting, receivers shouldn’t be used to create user workflows. They are only
tied to a list type, and any processing between different receivers, lists, and sites must be
managed by the developer. As code, they cannot be changed easily and are not accessible to
the users except to turn them on and off. As well, they have some quirks (as you’ll read later
in the chapter) that require double-checking statuses and other tasks that make them unusable
for some situations.
Within an Event Receiver, you can expose any or all of the events you need, based on
the way they fire and, of course, what you want to do. Since you have complete access (in
most cases) to what caused the event, you also can make decisions like “Only react to the
event if the list name is X” or “Only update the status if the status in another list is set.” The
basic event firing pattern is shown in Table 19-1.
As you can see, the naming of the methods matches the way that they fire, although you
have to watch out for those that fire additional events (such as an Add with an Attachment).
One thing to be aware of is how Event Receivers are executed within SharePoint. While
invoked by an event, you are running alongside the SharePoint process, and you’re not part
of it like a web part is. This means that your logic is based on background functionality, and
you cannot interact with the User Interface, prompt users for information, and so on. Also,
you don’t have a web context like you do in a web part; you must use the Properties object
in order to determine things such as the site you are running in. Basically, it’s like a standalone
application that simply uses SharePoint to start it up.
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace ListMetaLogger
{
public class ListMetaLogger : SPListEventReceiver
{
public override void FieldAdded(SPListEventProperties properties)
{
LogEvent(properties, “FieldAdded”);
}
public override void FieldAdding(SPListEventProperties properties)
{
LogEvent(properties, “FieldAdding”);
}
public override void FieldDeleting(SPListEventProperties properties)
{
LogEvent(properties, “FieldDeleting”);
}
public override void FieldDeleted(SPListEventProperties properties)
{
LogEvent(properties, “FieldDeleted”);
}
public override void FieldUpdated(SPListEventProperties properties)
{
LogEvent(properties, “FieldUpdated”);
72 CHAPTER 7 Handling List Events
}
public override void FieldUpdating(SPListEventProperties properties)
{
LogEvent(properties, “FieldUpdating”);
}
private void LogEvent(SPListEventProperties properties, string eventName)
{
StreamWriter sw = File.AppendText(@”C:\listeventlog.txt”);
StringBuilder sb = new StringBuilder();
sb.AppendFormat(“[{0}] {1} Event Occurred:\n”,
DateTime.Now.ToString(), eventName);
sb.AppendFormat(“\tList : {0}”, properties.ListTitle);
sb.AppendFormat(“\t{0} - Field {1}”,
properties.EventType.ToString(), properties.FieldName);
sw.WriteLine(sb.ToString());
sw.Close();
}
}
}