Search This Blog

Friday, December 19, 2014

Event Receivers in SharePoint 2010 : A complete Concept

1. Introduction

While developing any application we need to handle some events. For example, when a new item is added to a list, we may need to perform some action like say, notifying the person who created the list item, or modifying the dependent entries in some other location etc. Handling of such events is facilitated by event receivers. We have many SharePoint event receiver classes in order to handle variety of events.

2. SharePoint Event Receiver types

There are basically two types of event receivers: Synchronous and Asynchronous event receivers.Synchronous event receivers are also known as ‘Before’ event receivers. They are used to perform some custom action before an event occurs. In this case, there is a flexibility of cancelling the event on some conditions so that the event that gets initiated will not occur.
Asynchronous event receivers are also known as ‘After’ event receivers. They are used to perform some custom action after an event occurs. These events are asynchronous because they need not happen immediately after the event is performed. They can happen sometime later also, unlike synchronous event receivers which occur immediately before an event occurs.

3. Classes of Event Receivers

There are fundamentally five classes for Event Receivers for SharePoint 2007.
Microsoft.SharePoint.SPEmailEventReceiver : This provides event handling whenever a  user sends an email to a list on which e-mail feature is enabled
Microsoft.SharePoint.SPFeatureReceiver :  This provides event handling whenever is feature is acted upon
Microsoft.SharePoint.SPItemEventReceiver : This provides event handling whenever any action is performed on a list item
Microsoft.SharePoint.SPListEventReceiver : This provides event handling whenever a list definition is modified like adding or deleting columns and modifying existing columns.
Microsoft.SharePoint.SPWebEventReceiver : This provides event handling whenever a site or a site collection is deleted or moved
Each of the classes has some virtual methods which are to be over-ridden to perform our custom action. Those methods are as follows.
3.1 Site Level Events
Methods under the class SPWebEventReceiver handle site level events. A brief overview of those methods is as follows.
Site Deleted – This occurs after a site collection is deleted.
Site Deleting – This occurs before a site collection is being deleted.
Web Deleted – This occurs after a website has been deleted completely. This is an asynchronous after event.
Web Deleting – This occurs before a website is deleted. This is a synchronous before event.
Web Moved – This occurs after an existing website is moved. This is an asynchronous after event.
Web Moving – This occurs before a website is moved. This is a synchronous event.

3.2 List Level Events

Methods under the class SPListEventReceiver handle list level events. A brief overview of those methods is as follows.
Field Added – This occurs after a field link is added to a list.
Field Adding – This occurs when is field is being added to a content type.
Field Deleted – This occurs after a field is deleted from a list.
Field Deleting – This occurs when a field is being deleted from a list.
Field Updated – This occurs after a field is updated in a list.
Field Updating – This occurs when a field is being updated in a list.

3.3 Item Level Events

Methods under the class SPItemEventReceiver handle item level events. A brief overview of those methods is as follows.
ItemAdded – This occurs after a new item is added to a list. This is an asynchronous after event.
ItemAdding – This occurs before an item is added to a list. This is a synchronous before event.
ItemAttachmentAdded – This occurs after an attachment is added to a list. This is an asynchronous event.
ItemAttachmentAdding – This occurs while an attachment is being added to a list.
ItemAttachmentDeleted – This occurs after an attachment is removed from an item.
ItemAttachmentDeleting – This occurs while an attachment is being removed from an item.
ItemCheckedIn – This occurs after an item is checked in.
ItemCheckedOut – This occurs after an item is checked out.
ItemCheckingIn – This occurs while an item is being checked in.
ItemCheckingOut – This occurs while an item is being checked out.
ItemDeleted – This occurs after an item is deleted from its list.
ItemDeleting – This occurs while an item is being deleted from its list.
ItemFileConverted – This occurs when the type of file is being converted.
ItemFileMoved – This occurs after a file is moved.
ItemFileMoving – This occurs while a file is being moved.
ItemUncheckedOut – This occurs after un-checking an item in a list.
ItemUncheckingOut – This occurs while an item is being unchecked.
ItemUpdated – This occurs after an item is updated.
ItemUpdating – This occurs while an item is being updated.

3.4 Feature Events

Methods under the class SPFeatureReceiver handle events related to feature. A brief overview of those methods is as follows.
FeatureActivated – This occurs after a feature is activated.
FeatureDeactivating – This occurs when a feature is being deactivated.
FeatureInstalled – This occurs after a feature is installed.
FeatureUninstalling – This occurs when a feature is being uninstalled.

3.5 Email Events

Method under the class SPEmailEmailEventReceiver handles the event related to email. A brief overview of the method is as follows.
EmailReceived This occurs after an email message has arrived (to an email enabled list).

4. Binding the Event Receivers

For the event receivers to receive an event, they should be bound to the corresponding objects. This binding can be done in three ways.
Using a feature
Using a content type
Using WSS object model

4.1  Using Feature

In the feature, the elements.xml file should be as follows.
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
……………………
……………………
<Receivers ListTemplateId=”100″>
<Receiver>
<Name>VideoAddedEventReceiver</Name>
<Type>ItemAdded</Type>
<SequenceNumber>20000</SequenceNumber>
<Assembly>MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa</Assembly>
<Class>MyProject.MyModule.Events.VideoAddedEventReceiver</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
………………………………
……………………………..
</Receivers>
</Elements>
Any number of receivers can be bound through one feature.
Limitation: Site level events cannot be registered/bound through the feature because we cannot associate a ListTemplateId with a site.

4.2 Using  Content Type

In a content type, the elements.xml file should be as follows.
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
<ContentType ID=”id” Name=”name” Group=”GroupName” Description=”Description of content type” Version=”0″>
<FieldRefs>
<FieldRef ID=”FIELD ID” Name=”FIELD NAME” />
…………………………
…………………………
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/events“>
<spe:Receivers xmlns:spe=”http://schemas.microsoft.com/sharepoint/events“>
<Receiver>
<Name> VideoAddedEventReceiver </Name>
<Type> ItemAdded </Type>
<SequenceNumber>20000</SequenceNumber>
<Assembly> MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa </Assembly>
<Class> MyProject.MyModule.Events.VideoAddedEventReceiver</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</spe:Receivers>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>
The same limitation of ‘using feature’ applies to content type also because a content type cannot be associated with a site.
Sequence Number determines when the event receiver should get executed in case if more than one event receivers are about to be executed. An event receiver with greater sequence number gets executed after its counterpart with lesser sequence number.

4.3 Using WSS object model

The below is a sample code how an event receiver is bound with a list.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.Lists[“Video Library”].EventReceivers.Add(
SPEventReceiverType.ItemAdded,
“MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa”,
” MyProject.MyModule.Events.VideoAddedEventReceiver “);
}
In the above code, the custom class ‘VideoAddedEventReceiver’ of type ‘ItemAdded’ is bound with a list named ‘Video Library’.

5. Unbinding SharePoint Event Receiver

• Event receiver bound with the help of content type cannot be unbound.
• If the event receiver was bound with the help of a feature, deactivating the feature would unbind the event receiver from its associated object.
• If the event receiver was bound using WSS object model,
1. Move to the object (site collection or website or list) to which the event receiver is bound.
2. Retrieve the event receiver definition of the intended event receiver.
3. EventReceiverDefinitionObject.Delete().
4. Call the update of that object (site collection or website or list).
Event receiver base classes and supported events
Following is the list of Event Receiver base classes and the events supported by them:
Event receiver base classAvailable event host typesSupported events
SPWebEventReceiverSPSiteSiteDeleting
SPWebSiteDeleted
WebAdding
WebProvisioned
WebDeleting
WebDeleted
WebMoving
WebMoved
SPListEventReceiver (lists)SPSiteListAdding
SPWebListAdded
ListDeleting
ListDeleted
SPListEventReceiver (fields)SPSiteFieldAdding
SPWebFieldAdded
SPListFieldDeleting
SPContentTypeFieldDeleted
FieldUpdating
FieldUpdated
SPItemEventReceiverSPSiteItemAdding
SPWebItemAdded
SPListItemDeleting
SPContentTypeItemDeleted
ItemUpdating
ItemUpdated
ItemFileConverted
ItemFileMoving
ItemFileMoved
ItemCheckingIn
ItemCheckedIn
ItemCheckingOut
ItemCheckedOut
ItemAttachmentAdding
ItemAttachmentAdded
ItemAttachmentDeleting
ItemAttachmentDeleted
SPEmailEventReceiverSPSiteEmailReceived
SPWeb
SPList
SPWorkflowEventReceiverSPSiteWorkflowStarting
SPWebWorkflowStarted
SPListWorkflowCompleted
SPContentTypeWorkflowPostponed


SharePoint: Disable Event Receiver From non-receiver code

When we are in List Item Event Receiver code, we can modify/update the same item which will fire the event receiver again. For disabling event receiver to get fired again we can use the property ‘’ as shown below:
public class TestEventReceiver:SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        //disable event receiver firing
        EventFiringEnabled = false;
        
        
        //do something

 
 
        //enalbe event receiver firing
        EventFiringEnabled = true;

    }
}
Figure 1: Sample code to enable/disable event receiver firing inside event receiver handler. 
However, if you are in a webpart and want to modify an item but don’t want to fire event receiver, then?
 Don’t worry there’s a way out. I’ll explain this today.

 

What happens when Event Receiver disabled?

When you disable event receiver, SharePoint internally set a data field in current Thread. So if you can 
set your current’s thread’s data to the required value before/after updating item, you can control the event receiver. However, setting current thread data manually might be risky and I’ll not use that path. Rather 
I’ll show how we can use existing power of ‘SPEventReceiverBase’ to control event receiver firing.

Create your own Event Receiver Controller

I’ve create a custom class inherited from ‘SPEventReceiverBase’. From this base class I get a properties ‘EventFiringEnabled’ which allows me to control the event receiver firing. The following code snippet shows my custom EventReceiverManager:
public class EventReceiverManager : SPEventReceiverBase, IDisposable
{
    public EventReceiverManager(bool disableImmediately)
    {
        EventFiringEnabled = !disableImmediately;
    }

    public void StopEventReceiver()
    {
        EventFiringEnabled = false;
    }
    public void StartEventReceiver()
    {
        EventFiringEnabled = true;
    }

    public void Dispose()
    {
        EventFiringEnabled = true;
    }
}
Figure 2: A EventReceiverManager custom class to control event receiver firing. 
The code snippet above is derived from SharePoint’s SPEventReceiverBase to use the ‘EventFireingEnabled’ property.

Now you can use this EventReceiverManager to control the event receiver. To stop firing the event receiver on any changes, you need to wrap the code block inside EventReceiverManager as shown below:
using (var eventReceiverManager = new EventReceiverManager(true))
{
    var list = GetList("listName");
    var listItem = list.GetItemById(itemId);
    listItem["field"] = "value";
    listItem.Update();
}
Figure 3: How to use EventReceiverManager to disable event receiver firing. 
As shown above even if he list has event receiver for ItemUpdated/ItemDating the event receiver will not get fired because of putting the code in EventReceiverManager block. Please notice of ‘using’ block, as soon as you leave the ‘using’ block, the event firing in enabled automatically. This is because in Dispose method of EventReceiverManager I’ve enabled the event firing.

Event Receivers vs Workflows - Decide Which One to Use

As SharePoint Event Receivers & SharePoint workflows has lot of similarities, Many people stuck on deciding which one to go with: Event Receiver or Workflow? event receiver vs. workflow in SharePoint is always trick to decide.

Main Differences Between SharePoint Event Receivers and SharePoint Workflows are:

1. Event handlers Can't be manually initiated - workflows can be initiated either automatically or manually.

2. Event Handlers can be Synchronous or Asynchronous - Workflows are always async (They executes after the operation)

3. In Event Receivers we can cancel the operation (such as add/update/delete) - But in Workflows its not possible.

4. Event handlers execute from a Particular WFE, So when some thing goes wrong in that WFE, It may end-up. But Workflow Jobs are robust and  can resume even after Reboots.

5. Usually Event handlers runs for short period - Workflows can be longer even for years!

6. There is no User Interface/user Interaction in Event Receivers - Workflows can have user interactions such as getting user input in Initiation forms.

7. As the Name indicates, SharePoint Event receivers are triggered by events like New Item Adding-Added, Updating-Updated, Deleting-Deleted, etc. - But Workflows triggered only on Creation/Change/deletion.

8. Event Receivers are created using Visual studio - Workflows can be via SharePoint user interface, SharePoint Designer, Visio or Visual studio.

9. Workflows leaves "Workflow History" logs which we can refer for debugging - Event handler doesn't do such.

10. Event receivers are better for large volume - Workflows are better for small amount of data.

This one from Codeplex: SharePoint 2010 event receiver vs workflow

event receiver vs workflow

How to disabling event firing in SharePoint 2010

Today, I tested whether event receiver fires or not when I use SPListItem's Update methods.
The results are here.
Methods                 event firing
Update()                   fired
SystemUpdate()             fired

SystemUpdate(false)        fired
UpdateOverwriteVersion()   fired

All methods fired !!

But I think people sometimes want not to fire a event receiver when a update method is used.
So, I would like to introduce how to disable event firing.


In SharePoint 2010, SPEventReceiverBase.DisableEventFiring Method and SPEventReceiverBase.EnableEventFiring Method are already obsolete. We need to use EventFiringEnabled Property.

SharePoint 2010 Event Receiver Interview Questions

1.         What is event receiver?
An event receiver is a piece of managed code that responds to SharePoint events when specific triggering actions occur on a SharePoint object. Triggering actions include activities such as adding, updating, deleting, moving, checking in, and checking out.

2.         What Are Event Hosts?
Event hosts are common SharePoint objects that expect to receive events—in other words, objects whose event receivers "listen" for SharePoint events. These SharePoint event host object types include instances of common objects such as SharePoint site collections, sites, and lists. Each event host type has a specific set of event receiver base types from which it can inherit.

3.         What are the types of event receivers?
There are two types of event receivers available in SharePoint.
1. Syncronous Events: This event is executed in the same thread in which the triggering action is occurring. For example, when the user adds an item from the SharePoint user interface, the event is fully executed before returning to the user and showing that the item was added. These are before events that occurs before the currently requested operation happens.  This provides an opportunity for an event receiver to perform tasks before data is committed to a database. A good example of the use of Before events is performing data validation, because Before events fire prior to commitment of data. You can also use Before (or synchronous) events to cancel user actions—for example, if data validation fails.
(e.g. ItemAdding, ItemUpdating,ItemDeleting)

2. Asynchronous Events: This event occurs at a later time than the action that triggered it, and it executes on a thread that is different from the one in which the triggering action is running. These are after events. After events trigger event receivers that execute after user actions are committed to the content database; they invoke code that runs after the content database has been modified. This allows you to execute logic that occurs after a user has completed a specific action.
(e.g. ItemAdded, ItemUpdated,ItemDeleted)

4.         What is the base class for List events and List Item events?
The Base class for SharePoint events is   Microsoft.SharePoint.SPEventReceiverBase. The List and List Item events inherited based on the scope of event host as below.
   List Item: 
Microsoft.SharePoint.SPItemEventReceiver
  SharePoint List:  Microsoft.SharePoint.SPListEventReceiver

5.         How to cancel the actions using event receiver?
SharePoint before events(synchronous) can be cancelled since it get trigged before completing the operation. Any validation can be applied as well as the event can be cancelled using event properties.
E.g
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if(condition=true)
{
properties.Cancel=True;
}
}

6.         What is the best practice to get the context of SPSite when using event receiver?
The event properties should be used to get the current SPContext. If we get the current context of the site in different object  like SPContext then your connection may get lose when your dispose the site context.  Use SPItemEventProperties.Site/SiteID properties to get the current context or use SPItemEventProperties.Context property.

7.         Best coding practice of event receiver?
a.       Enable / disable firing other event in the same SharePoint object to avoid unnecessary loops.
b.      Do not use SPcontext to get the site/web properties, instead user event properties to get site/web properties
c.       Dispose if any heavy object process in event receiver in final block
d.      Do not use RunWithElevatedPrevillege if event receiver planned to be deployed as sandbox solution
e.      Use UserToken to impersonate your current access.

8.         What is Enabling and Disabling event receiver?
SharePoint Event is nothing but triggering action occurs on a SharePoint object. The trigger may come from manual action (updating item manually) or automatic action (SPItem.Update). Both actions will fire the events. Sometimes it makes unnecessary events in the same list. These events many go infinitely and impact the server performance majorly. To avoid this unnecessary event firing, we can purposely enable/disable the other event firing on the same object. 
You can achieve this by executing the DisableEventFiring method on the list item. After the changes are saved you can activate the event handler again by executing the EnableEventFiring method on the list item.
Best practice is to execute the DisableEventFiring method right before any of the update methods and to execute the EnableEventFiring method right after one of the update methods.

9.         What is before property?
Get the string/value pairs in a hash table before the event occurred on the SharePoint item.

10.     What is after property?
Get the string/value pairs in a hash table after the event occurred on the SharePoint item.

11.     What is the difference between event receiver and workflow?
Workflows:
·         SharePoint Workflow consist a set of task(s) which can be triggered both manually and automatically.
·         SharePoint Workflows are created in SharePoint Designer, Microsoft Visio and of course through Visual Studio.
·         The workflow is triggered once an action is completed.
·         Workflow can be exported and imported
·         Workflow can be paused
·         Workflow can send mails without attachment
·         Does not required coding knowledge to work on SPD workflows
·         Multiple forms(initiation, task edit..) can be associated as part of workflow
Event Receivers:
·         SharePoint event receiver contains custom coding/logic which can be triggered automatically based on the event host type.
·         SharePoint event receivers can be created only in Visual Studio.
·         The events can be triggered before action or after action.
·         Events cannot be exported and imported
·         Events can be cancelled(synchronous) but cannot be paused
·         Even receivers  can send mails with attachment
·         Required coding knowledge
·         No custom forms associated as part of event actions

12.     Can SharePoint events cancelled and redirected to differ page?
Yes, SharePoint Synchronous events can be cancelled since it occurs before action on SharePoint object. So the event can be cancelled and redirects to different page.
Continue:  The event is allowed to continue.
CancelNoError: The event is cancelled but no error message is displayed.
CancelWithError: The event is cancelled and an error message is displayed.
CancelWithRedirectUrl Obsolete: The event is cancelled, but a URL is provided that redirects
the user to a custom error message page, or to any desired URL.
e.g.
public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           properties.Cancel = true;
           properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
           properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";
           this.EventFiringEnabled = false;
       }
13.     How to add an event receiver to the specific list programmatically/object model?
The below is a sample code how an event receiver is bound with a list.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.Lists["Video Library"].EventReceivers.Add(
SPEventReceiverType.ItemAdded,
“MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa”,
” MyProject.MyModule.Events.VideoAddedEventReceiver “);
}
In the above code, the custom class ‘VideoAddedEventReceiver’ of type ‘ItemAdded’ is bound with a list named ‘Video Library’.


14.     Why can’t we use SPContext in even receiver?
The event properties should be used to get the current SPContext. If we get the current context of the site in different object  like SPContext then your connection may get lose when your dispose the site context.  Use SPItemEventProperties.Site/SiteID properties to get the current context or use SPItemEventProperties.Context property.

15.     Can event receiver deployed as sandbox solution?
Yes, Event receivers can be deployed as sandbox solution. But make sure that your custom code is not using RunWithElevatedPrevillege method. Sandbox solution only can access the site collection object and it cannot access other than site collection object, network resources, database

16.     What is event binding OR how to register events on SharePoint Object?
For the event receivers to receive an event, they should be bound to the corresponding objects. This binding can be done in three ways.
Using a feature
Using a content type
Using WSS object model

17.     What is event unbinding and how to unbind?
• Event receiver bound with the help of content type cannot be unbound.
• If the event receiver was bound with the help of a feature, deactivating the feature would unbind the event receiver from its associated object.
• If the event receiver was bound using WSS object model,
1. Move to the object (site collection or website or list) to which the event receiver is bound.
2. Retrieve the event receiver definition of the intended event receiver.
3. EventReceiverDefinitionObject.Delete().
4. Call the update of that object (site collection or website or list).

18.     How to find event receiver is attached to an list/library?
Registered events can be fetched using server object model or PowerShell scripts

19.     What is best either event receiver or workflow?
Its based on requirement. If business need very simple operation to be done with any custom calculation then workflow is the best option. If business need some custom business logic OR some action on before event then event receivers is best choice.

20.     Can event receiver register using feature?
Yes

21.     Can more than one event receiver register on same list/library?
Yes, the event receiver sequence specifies the order in which an event receiver is executed in cases where an event triggers multiple event receivers. For example, if you have two ItemAdded event receivers bound to the same list (one from Assembly "1" and the other from Assembly "2"), the event receiver that is bound with a lower sequence number is executed first. A practical example is adding an event receiver to a system list that already has a system event receiver bound to it. In that case, you assign the new event receiver a higher sequence number.

22.     If more than one event receiver registered on single list/library then how it works?
The event receiver sequence specifies the order in which an event receiver is executed in cases where an event triggers multiple event receivers. For example, if you have two ItemAdded event receivers bound to the same list (one from Assembly "1" and the other from Assembly "2"), the event receiver that is bound with a lower sequence number is executed first. A practical example is adding an event receiver to a system list that already has a system event receiver bound to it. In that case, you assign the new event receiver a higher sequence number.

23.     Where event receiver is deployed?
Events can be deployed on both sandbox solution and farm solution. If we use sandbox solution then it will deployed on site collection solution gallery and required DLL will be  extracted on UCCache folder when needed. If event receiver deployed as farm solution then the DLL registered in GAC and solution will be available in farm solution gallery.

24.     How to add an event receiver to the specific content type?
In a content type, the elements.xml file should be as follows.

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
<ContentType ID=”id” Name=”name” Group=”GroupName” Description=”Description of content type” Version=”0″>
<FieldRefs>
<FieldRef ID=”FIELD ID” Name=”FIELD NAME” />
…………………………
…………………………
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/events“>
<spe:Receivers xmlns:spe=”http://schemas.microsoft.com/sharepoint/events“>
<Receiver>
<Name> VideoAddedEventReceiver </Name>
<Type> ItemAdded </Type>
<SequenceNumber>20000</SequenceNumber>
<Assembly> MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa </Assembly>
<Class> MyProject.MyModule.Events.VideoAddedEventReceiver</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</spe:Receivers>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>

The same limitation of ‘using feature’ applies to content type also because a content type cannot be associated with a site.
Sequence Number determines when the event receiver should get executed in case if more than one event receivers are about to be executed. An event receiver with greater sequence number gets executed after its counterpart with lesser sequence number.

25.     How to add an event receiver to the content type using feature.xml file?
In the feature, the elements.xml file should be as follows.

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
……………………
……………………
<Receivers ListTemplateId=”100″>
<Receiver>
<Name>VideoAddedEventReceiver</Name>
<Type>ItemAdded</Type>
<SequenceNumber>20000</SequenceNumber>
<Assembly>MyProject.MyModule.Events, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b231a5ff63cd9fa</Assembly>
<Class>MyProject.MyModule.Events.VideoAddedEventReceiver</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
………………………………
……………………………..
</Receivers>
</Elements>

Any number of receivers can be bound through one feature.
Limitation: Site level events cannot be registered/bound through the feature because we cannot associate a ListTemplateId with a site.

26.     What are the new event receivers added in SharePoint 2010?
Event Host Type
Existing Events
New Events
Events Base Class
SPSite
SPWeb
SiteDeleting
SiteDeleted
WebDeleting
WebDeleted
WebMoving
WebMoved
WebAdding
WebProvisioned

SPWebEventReceiver
SPSite
SPWeb

ListAdding
ListAdded
ListDeleting
ListDeleted
SPListEventReceiver

SPSite
SPWeb
SPList
SPContentType
FieldAdding
FieldAdded
FieldDeleting
FieldDeleted
FieldUpdating
FieldUpdated

SPListEventReceiver

SPSite
SPWeb
SPList


EmailReceived
SPEmailEventReceiver
SPSite
SPWeb
SPList
SPContentType

WorkflowStarting
WorkflowStarted
WorkflowCompleted
WorkflowPostponed
SPWorkflowEventReceiver
SPSite
SPWeb
SPList
SPContentType
ItemAdding
ItemAdded
ItemDeleting
ItemDeleted
ItemUpdating
ItemUpdated
ItemFileConverted
ItemFileMoving
ItemFileMoved
ItemCheckingIn
ItemCheckedIn
ItemCheckingOut
ItemCheckedOut
ItemAttachmentAdding
ItemAttachmentAdded
ItemAttachmentDeleting
ItemAttachmentDeleted

SPItemEventReceiver



SharePoint Synchronous Event Receiver with 

message to UI and saving form data




The goal is to save data from the new/edit form to list item and in certain situations show a message to UI by using event receivers.


As we all know showing a message to UI is not going to work when using async event receivers. So we’re stuck to use sync event receivers. At first you will see examples that shows a message by using code like this:

try
{
    //some code
}
catch (Exception ex)
{
    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage = ex.Message;
    properties.Cancel = true;
}


The options for properties.Status are:
Continue, there is no error.
CancelNoError, silently cancel the request as if the request has succeeded.
CancelWithError, cancel the request with the error message specified in ErrorMessage.
CancelWithRedirectUrl cancel the request and redirect to the URL specified in RedirectUrl.
The only usable is CancelWithRedirectUrl. However this will not save your form data to the listitem. So all changes are lost.
Where is the option to ‘ContinueWithRedirectUrl’? It doesn’t exist.
Luckily we have the SPUtility class with the Redirect method. When using the SPRedirectFlag.DoNotEndResponse flag
 it will not abort the event receiver thread (Flag Default will abort event receiver thread and therefor will not save the afterproperties on the list item.). In this case the event receiver thread will be completed and afterproperties are save to the list item. And the redirect will be processed. You could redirect a user to a custom page, provide some parameters and show a message as you like.
Code sample of the redirect:

SPUtility.Redirect("notificationpage.aspx?status=somestatusvalue", SPRedirectFlags.DoNotEndResponse, HttpContext.Current);





SharePoint 2010: Event Receiver ItemAdding and ItemUpdating How To Set AfterProperties DateTime Field Value

If you are receiving this error “Invalid date/time value. A date/time field contains invalid data. Please check the value and try again.”. This is because the assigning value for the AfterProperties DateTime field in the ItemAdding or ItemUpdating event in the Event Receiver was not formatted correctly.


To resolve this issue you need to format the DateTime value to an ISO8601 string format:
properties.AfterProperties["StartDate"] = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
properties.AfterProperties["StartDate"] = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);

WebProvisioned event receiver

The WebProvisioned web event receiver is a new receiver type introduced in SharePoint 2010.
 It is one very useful addition to the already extensive suite of event receiver types in SharePoint, 
as it allows you to add some additional logic when a sub-site in your site collection gets created. 
In this sense it resembles to some extent the feature stapling functionality, but it differs from 
it because its scope is the site collection level.

Practical Scenario:

You are designing a SharePoint 2010 intranet site at your company. The Finance department has designed a SharePoint List. They need this list to be included in any new site that is created by 
using the Team Site site definition provided by SharePoint. A feature has been created that 
provisions the list in a site. We need to satisfy the request from the Finance department while 
reducing the risk that future SharePoint service pack updated will impact the solution.

The best approach is:
Create an event handler and register it with the Web Provisioned event. Activate the feature in 
the event handler.

EVent Reciever Create Folder In Sharepoint 2010
I have this event receiver c# class that I am trying to implement on a Sharepoint site. It did not work. I have deployed it from visual studio 2010 after it was build ok. Does anyone see what is the problem? Is the code ok? or is the problem on the SP? Thank you. - here is the new code
    using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverCFolder.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {

            try
            {
                if (properties.ListTitle == "CF") // list where the item was added 
                { // if item was added to this list then create a folder on -  Dlib - list
                    UpdateFolder(properties);
                }
            }
            catch (Exception ex)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = ex.Message;
                properties.Cancel = true;
            }
        }

        private void UpdateFolder(SPItemEventProperties properties)
        {
            string foldername = properties.ListItem["Title"].ToString();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //inside RunWithElevatedPriviliges I need to open a new site (an elevated site) 
                    using (SPSite site = new SPSite(properties.Web.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList list = web.Lists.TryGetList("DLib"); // this is doc Library where the new folder will be created
                            //note that we are creating a list item, not a folder - a folder IS a list item 
                            SPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
                            if (createdFolder != null)
                            {
                            createdFolder["Name"] = foldername;
                                createdFolder.Update();
                            }
                            list.Update();
                        }
                    }
                });
            }
            finally { }
        }
    }
}

Don't do this: SPUser privilegedAccount = properties.Web.AllUsers[@"SHAREPOINT\SYSTEM"]; Read up on using SPSecurity.RunWithElevatedPrivileges. See the MSDN documentation here.
Also don't do a using (SPSite... and inside the using block you try to get the web via SPContext.Current - that web won't be elevated anymore.
The correct way is something along these lines (I didn't try this, so it' just to give you an idea where you are headed):
private void UpdateFolder(SPItemEventProperties properties)
{
    string foldername = properties.ListItem["Title"].ToString();

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
            //inside RunWithElevatedPriviliges I need to open a new site (an elevated site)
        using (SPSite site = new SPSite(properties.Web.Site.ID))
        {
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists.TryGetList("ListTitle"); //is that really the list title?
                //note that we are creating a list item, not a folder - a folder IS a list item
                SSPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
                if (newFolder != null)
                {
                    createdFolder["Name"] = foldername;
                    createdFolder.Update();
                }
                list.Update();
            }
        }
    });
}

Event Receiver not firing when document is created using Office


Problem Description:
If you create a new document using Office client applications (such as Word, Excel, PowerPoint) and save it into SharePoint, event receivers do not fire. 
Common belief: This is because “adds from Office only fire ItemAdded, not ItemUpdated”. (Please note that this statement is not correct.)
Correction: Adds from Office client applications fire all registered event receivers at all times. However, since they fire with security contextof triggering user (normal users), most of the time that user has no admin rights to perform operations such as creating subsite, modifying site, web objects, sending email … etc. This makes users think that event receivers do not fire.
Here are all item events:
  • ItemAdded
  • ItemAdding
  • ItemAttachmentAdded
  • ItemAttachmentAdding
  • ItemAttachmentDeleted
  • ItemAttachmentDeleting
  • ItemCheckedIn
  • ItemCheckedOut
  • ItemCheckingIn
  • ItemCheckingOut
  • ItemDeleted
  • ItemDeleting
  • ItemFileConverted
  • ItemFileMoved
  • ItemFileMoving
  • ItemUncheckedOut
  • ItemUncheckingOut
  • ItemUpdated
  • ItemUpdating
Let’s take a look at the sequence event are fired. 
While uploading a new document in SharePoint library:
Case-1: If a document library does not force check-out, here is the sequence of item events fired.
Case-2: If a document library forces check-out, here is the sequence of item events fired.
Note: ‘..ing’ events are sync before-events. ‘…ed’ events are asynch after-events.
While creating a new document using Office client applications:
(such as Word, Excel, PowerPoint)
Case-1: If a document library does not force check-out, here is the sequence of item events fired.
Case-2: If a document library forces check-out, here is the sequence of item events fired.
Demo:
Let’s deep dive into the issue: 
Event Receiver not firing while adding a document using Office client applications”
In this demo, I’ll have a code snippet to update current web’s description each time an event fires as seen below.
Doing so, not only we get to see which events fire but also we’ll observe the sequence they fire. Remember before-events are sync events, however after-events are asynch events. This means that execution sequence of after-events may vary with different executions of same code.

Usage-1: No Try-Catch, opening web object from event properties object

Create a new document
Type something and save it
Debug and put a breakpoint at web.Update().
Go ahead and execute this line, you’ll see that an exception is thrown.
Important Finding:
Reason we get a security exception here is that event receiver’s ‘properties’ object was created using current user who has no privileges to update web object.
Misleading fact:
Since there is no exception handling here, thrown exception is not caught by event receiver code. Execution continues and end-user gets no exception, which makes end-user think event receiver is not firing.
What to do:
Always use exception handling in your event receiver code. Handle any exceptions thrown accordingly.

Usage-2: Using Try-Catch, opening web object from event properties object

Follow same steps above to create a test doc. This time we’ll catch the exception.
Now, we are aware that something went wrong. It’s an “Access Denied” error, due to security context of current user. Current user has no rights to modify web object, which is normal behavior to expect.

Usage-3: Using Try-Catch, opening web object from event properties object, and running code with elevated privileges

This time let’s try to run the code with elevated privileges to solve security context problem.
Again, create another test document and debug.
Now, we see here something really interesting:
Access Denied error while running with Elevated Privileges!!!
Important Finding:
Even though code is run with elevated privileges, we still get a security exception. This is because event receiver’s ‘properties’ object is still created using current user’s security context.
Misleading fact:
Running code with elevated privileges does NOT always guarantee that you will not get an “Access Denied” error. Security context of objects are still to be checked even though your code executes with elevated privileges.
What to do:
Always use ‘Using’ clauses when running your code with Elevated Privileges in order to avoid security related exceptions.

Usage-4: Using Try-Catch, opening web object using ‘Using’ clauses, and running code with elevated privileges

Finally, let’s add using clauses to the code. Doing so, we ensure that a brand new web object is created while running code with elevated privileges.
Create another test document and debug.
As seen above, this time web.Update() method is executed successfully!

In SharePoint 2010, there are many new event handlers that can be leveraged to interject our custom code that will take effect when end users perform various actions. There are around 12 new event handlers available.

New Event Handlers in SharePoint 2010

List Events
Existing Ones
  • A field was added
  • A field is being added
  • A field was removed
  • A field is being removed
  • A field was updated
  • A field is being updated
New Additions
  • A list is being added
  • A list is being deleted
  • A list was added
  • A list was deleted
List Item Events
Existing Ones

  • An item is being added
  • An item is being updated
  • An item is being deleted
  • An item is being checked in
  • An item is being checked out
  • An item is being unchecked out
  • An attachment is being added to the item
  • An attachment is being removed from the item
  • A file is being moved
  • An item was added
  • An item was updated
  • An item was deleted
  • An item was checked in
  • An item was checked out
  • An item was unchecked out
  • An attachment was added to the item
  • An attachment was removed from the item
  • A file was moved
  • A file was converted 
New Additions
  • The list received a context event
List Workflow Events
This is very new in 2010; these events are not available in SharePoint 2007:
  • A workflow is starting
  • A workflow was started
  • A workflow was postponed
  • A workflow was completed
List Email Events
This is very new in 2010; these events are not available in SharePoint 2007:
  • The list received an e-mail message
Feature Events
There are no additions
  • A feature was activated
  • A feature is deactivating
  • A feature was installed
  • A feature is being upgraded
Web Events
Existing Ones
  • A site collection is being deleted
  • A site is being deleted
  • A site is being moved
  • A site collection was deleted
  • A site was deleted
  • A site was moved
New Additions

  • A site is being provisioned
  • A site was provisioned
Programatically Or Practically  Event Receiver Step By Step


1 comment:

  1. Hi,
    Thanks for your valuable post. Can you please tell me how to implement eventreceiver for itemadded.
    When we create a list item in ListA in SiteCollectionA, the item has to be created automatically in ListB in SIteCollectionB.

    Please respond kindly

    ReplyDelete