SPSBMORE 2013 - Getting Started with Event Receivers

1,103 views

Published on

Slides of my session on SharePoint 2010 Event Receivers at SPSBMORE 2013 in in Room 21 at 03:30 PM.
Location :
Loyola University
Timonium Campus
2034 Greenspring Drive
Timonium, MD 21093

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,103
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
3
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

SPSBMORE 2013 - Getting Started with Event Receivers

  1. 1. #SPSBMORE @PGBhoyar Presented By: Prashant G Bhoyar Getting Started with Event Receivers in SharePoint 2010 18 May 2013
  2. 2. #SPSBMORE @PGBhoyar Who am I?
  3. 3. #SPSBMORE @PGBhoyar About today’s Session • Raise your hand if something is not clear • Sharing is Caring • Ask Questions • Giveaway : SharePoint 2010 book if you answer the question in the end • Let the learning begins…
  4. 4. #SPSBMORE @PGBhoyar What Will We Cover Today? • What are Event Receivers? • Common business scenarios for event receivers • Event receivers architecture • Developing event receivers • Various approaches to registering event receiver • Common issues and fixes for event receivers • Cancelling events and redirecting users to error pages • Event receivers best practices • When not to use them
  5. 5. #SPSBMORE @PGBhoyar What are Event Receivers? • Provides developers an ability to execute custom code against various events that occur during the life cycle of various SharePoint objects • SPSite • SPWeb • SPList • SPListItem • SPContentType
  6. 6. #SPSBMORE @PGBhoyar Object Operations Site Collection • Deletion Web • Creation • URL modification • Deletion List • Creation • Deletion • Received an e-mail message Field • Creation • Update • Deletion Item • Creation • Update • Deletion • Check in • Check out • File movement • File conversion • Adding attachment • Deleting attaching Workflow • Start • Completed • Postponed Operations Available to Event Receivers
  7. 7. #SPSBMORE @PGBhoyar LessComplexity Examples of Event Receivers in SharePoint • Auditing • Validations • Notifications • Various other Automations
  8. 8. #SPSBMORE @PGBhoyar LessComplexity Architecture of Event Receivers • Event Receiver Base Class: Inherit custom event receivers from one of the base classes • SPWebEventReceiver • SPListEventReiver • SPItemEventReceiver • SPWorkflowEventReceiver • SPFeatureReceiver • SPEmailEventReceiver
  9. 9. #SPSBMORE @PGBhoyar LessComplexity Event Receivers Base Class Structure
  10. 10. #SPSBMORE @PGBhoyar Event receiver base class Available event host types Supported events SPWebEventReceiver •SPSite •SPWeb •SiteDeleting •SiteDeleted •WebAdding •WebProvisioned •WebDeleting •WebDeleted •WebMoving •WebMoved SPListEventReceiver (lists) •SPSite •SPWeb •ListAdding •ListAdded •ListDeleting •ListDeleted SPListEventReceiver (fields) •SPSite •SPWeb •SPList •SPContentType •FieldAdding •FieldAdded •FieldDeleting •FieldDeleted •FieldUpdating •FieldUpdated Event Receiver Base Classes and Supported Events
  11. 11. #SPSBMORE @PGBhoyar Event receiver base class Available event host types Supported events SPItemEventReceiver •SPSite •SPWeb •SPList •SPContentType •ItemAdding •ItemAdded •ItemDeleting •ItemDeleted •ItemUpdating •ItemUpdated •ItemFileConverted •ItemFileMoving •ItemFileMoved •ItemCheckingIn •ItemCheckedIn •ItemCheckingOut •ItemCheckedOut •ItemAttachmentAdding •ItemAttachmentAdded •ItemAttachmentDeleting •ItemAttachmentDeleted SPEmailEventReceiver •SPSite •SPWeb •SPList •EmailReceived SPWorkflowEventReceiver •SPSite •SPWeb •SPList •SPContentType •WorkflowStarting •WorkflowStarted •WorkflowCompleted •WorkflowPostponed Event Receiver Base Classes and Supported Events Cont..
  12. 12. #SPSBMORE @PGBhoyar Typical Event Receiver Life Cycle
  13. 13. #SPSBMORE @PGBhoyar LessComplexity Synchronous and Asynchronous Events • Synchronous events provide an opportunity to act on the event and ability to change the outcome of it (such as canceling the event). • Before Events • Events ending with “ing” • Asynchronous events provide an opportunity to react to an event that just happened • After Events • Events ending with “ed”
  14. 14. #SPSBMORE @PGBhoyar LessComplexity Synchronous Events • Gets executed before data is committed to Content Database • Provides an opportunity to perform various pre processing tasks like validations • Provides an opportunity to cancel the event and nothing will get committed to content database • Run in the same process and thread that triggers the event • Avoid using complex time-consuming processing logic inside synchronous events as they block the execution of the current request thread until the event receiver completes its execution • UI will be held up in rendering • Tip: In case of list event receivers, do not try to reference “ID” of the current item that is getting created, since SharePoint assigns ID after the data is committed to Content Database
  15. 15. #SPSBMORE @PGBhoyar LessComplexity Asynchronous Events • Gets executed after data is committed to Content Database • Can’t cancel these events • Provides an opportunity to perform various post processing tasks like sending notifications • Run on a background thread and never block the UI thread • To change this behavior update the synchronization property of event receiver to synchronous • Scenario : update the UI with info from post execution of an asynchronous event receiver
  16. 16. #SPSBMORE @PGBhoyar LessComplexity SharePoint Events Pipeline • Synchronous events are processed in the same thread of user action. • Asynchronous events are processed on secondary threads.
  17. 17. #SPSBMORE @PGBhoyar LessComplexity Custom Event Receivers needs to be deployed and registered with SharePoint. Two options are available. • Declarative by using SharePoint Feature • Pros: Easy to deploy and maintain • Cons: Difficult to develop • Programmatically using Server side object model • Pros : Flexible, Easier to Develop • Cons : Difficult to deploy and maintain Deployment and Registration of Custom Event Receivers
  18. 18. #SPSBMORE @PGBhoyar DEMO
  19. 19. #SPSBMORE @PGBhoyar LessComplexity Developing Custom List Event Receivers • Set up the solution • Feature Activation • Event Receiver Code • Examples • Event Recursion • Event Cancellation
  20. 20. #SPSBMORE @PGBhoyar Best Practices • Always implement Exception Handling • No need to provision new SPWeb, SPSite objects of the current site, already available with properties. • Avoid complex time consuming processing logic • Use declarative approach for registration and deployment • Use sequence number above 10,000 to avoid conflict with SharePoint OOB event receivers • Avoid cascading events receivers
  21. 21. #SPSBMORE @PGBhoyar When not to use Event Receivers? • To perform long-running and processor-intensive tasks • Example : document conversion from MS Word to PDF • Alternatives: Timer Jobs, Batch Jobs • To Execute validation logic inside event receivers, which can easily be done on the client side • Alternatives : Use column-level validators to perform client-side validations • To Implement workflow-like process logic. • Alternatives : SharePoint Workflows
  22. 22. #SPSBMORE @PGBhoyar LessComplexity Is Event Receiver Right Choice? • Yes if, • OOTB options are not available • No Human Interaction in Automation • Data needs to be updated real time • Not doing complex time-consuming processing logic • No Chain of Events Receivers
  23. 23. #SPSBMORE @PGBhoyar References Appendix/Resources Expert SharePoint 2010 Practices By : Sahil Malik, Dan Bakmand-Mikalski, Razi bin Rais and Darrin Bishop MSDN: http://msdn.microsoft.com/en-us/sharepoint/default.aspx http://msdn.microsoft.com/en-us/library/gg749858.aspx http://msdn.microsoft.com/en-us/library/gg981880.aspx http://msdn.microsoft.com/en- us/library/microsoft.sharepoint.spitemeventreceiver.aspx
  24. 24. #SPSBMORE @PGBhoyar Questions? Feedback? Contact me:  Twitter: @PGBhoyar  Blog: http://pgbhoyar.wordpress.com (limited contents)  Email: pgbhoyar@gmail.com Thank You Organizers, Sponsors and You for Making this Possible.

×