Skip to main content

Microsoft Office SharePoint Server 2007 (MOSS)


Overview

Today I want to talk in depth about a feature of Microsoft Office SharePoint Server 2007 (MOSS) that shows you just how extensible the SharePoint 2007 platform is! That feature is the ability to add custom event handlers to sites, lists, items and content types in your portal.

What is a custom event handler? A custom event handler is a .Net assembly that contains the additional business logic you need to run when the event occurs in SharePoint.

Why did I pick this feature? Well, no portal or business application ever really lives in isolation to your other Line of Business (LOB) applications, and shouldn't! Your portal solution needs to be dynamic, expect change and work seamlessly with the other systems in your environment. So, you may ask, what does this have to do with Event Handlers?

Well, event handlers allow you to add [SOMETHING] your business needs into the functionality of SharePoint. [SOMETHING] examples:

o    Retrieve information from a database such as filling in the remaining fields of a list based on a CustomerID (assuming you don't have an enterprise license for BDC)
o    Call a web service
o    Start a workflow
o    Perform list data validation
o    Convert document to PDF and store in alternative list.
o    Access data in another list.

The possibilities around writing custom event handlers to perform an action based on an event in SharePoint 2007 are endless!

Event Types

Here is a list of events that you can hook into:

·         Microsoft.SharePoint.SPWebEventReceiver : "Site Level"

Occurs after a site collection has been deleted.
Occurs when a site collection is being deleted.
Asynchronous Afterevent that occurs after an existing Web site is completely deleted.
Synchronous before event that occurs before an existing Web site is completely deleted.
Asynchronous after event that occurs after an existing Web site has been moved.
Synchronous before event that occurs before an existing Web site has been renamed or moved to a different parent object.


·         Microsoft.SharePoint.SPListEventReceiver : "List Level"

Occurs after a field link is added.
Occurs when a field link is being added to a content type.
Occurs after a field has been removed from the list.
Occurs when a field is in the process of being removed from the list.
Occurs after a field link has been updated
Occurs when a field link is being updated

·         Microsoft.SharePoint.SPItemEventReceiver : "List Item Level"

Asynchronous After event that occurs after a new item has been added to its containing object.
Synchronous before event that occurs when a new item is added to its containing object.
Asynchronous after event that occurs after a user adds an attachment to an item.
Synchronous before event that occurs when a user adds an attachment to an item.
Asynchronous after event that occurs when after a user removes an attachment from an item.
Synchronous before event that occurs when a user removes an attachment from an item.
Asynchronous after event that occurs after an item is checked in.
Asynchronous after event that occurs after an item is checked out.
Synchronous before event that occurs as a file is being checked in.
Synchronous before event that occurs after an item is checked out.
Asynchronous after event that occurs after an existing item is completely deleted.
Synchronous before event that occurs before an existing item is completely deleted.

Occurs after a file is moved.
Occurs when a file is being moved.
Synchronous before event that occurs when an item is being unchecked out.
Synchronous before event that occurs when an item is being unchecked out.
Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.
Synchronous before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.


Asynchronous vs Synchronous Events: The "ing" and the "ed"
As you may have noticed above, there are two events raised for each type of event. The "...ing" event occurs before the action starts and the "...ed" occurs after the actions ends. "...ing" events occur synchronously while the "...ed" events occur asynchronously. What does this mean?

Synchronous events:
·         Occur before the event.
·         Block the flow of code execution until your event handler completes.
·         Provide you with the ability to cancel the events resulting in no after event (“...ed") being fired.

Asynchronous events:
·         Occur after the event.
·         Do not block the flow of code execution in SharePoint.


Combining Custom Event Handlers with Content Types = POWER!

Content Type is just what it sounds like: a metadata description of content that can include custom properties, a retention policy, and an associated set of workflows and business processes.
For example, a company may define a “Contract” Content Type that includes the required metadata to make it easy to find that contract later.
The “Contract” Content Type is now a metadata description definition that can be associated to lists such as Document Libraries in your SharePoint environment. Once added, you now have the ability to add and manage Contracts added to your portal.
This is where Event Handlers come in: You can attach custom event handlers to a Content Type! Once attached to the Content Type definition, it picks up the events wherever you use the content type. This is extremely powerful! Why? Well, you have the ability to target individual content types across the entire site collection. For example, wherever a “Contract” Content Type is used, you could store a link in another system for each Contract created in your system, no matter what document library the contract is stored in.
Best Practices 

When building custom event handlers, keep the following points in mind:

1. Security:
The assembly you deploy to the Global Assembly Cache(GAC) is running with full trust. Watch out for the following:

·         Denial of Service attack: If a user uploads 10000 items to a list, what is the effect it will have to the systems your assembly uses.
·         SQL Injection attack: If a user attempts to insert SQL statements into a column of a list that your assembly uses to update a database, what mechanisms are you using to make sure the input is valid.
·         Cross Site scripting attack: What is the effect of adding script to a column that your assembly uses to update another area in SharePoint?

2. Performance:
Watch out for the following:

·         Load: What burden are you placing on your web front end servers to run the assembly each time an event occurs? Use performance counters to determine this.
·         Long Running Operations: Consider creating a custom SharePoint timer job. Kick off/ Schedule the Timer Job from the event rather than running all the logic inside the event. This will allow you to use the features of SharePoint to view whether the Timer Job ran successfully.
·         Sync vs Async Events: Synchronous events have to wait until your code completes before returning the page, whereas Asynchronous events show the page immediately.

3. Error Handling:
When using synchronous events and you decide to cancel an event, let the user know why the event was cancelled. For example, update a Task List with a message why it failed.

Log your errors so that you can determine what issue is occurring when your system is in production environment.

4. Connections: Cater for external systems being down when you are trying to connect to them. Don’t let your code / solution go belly up because you cannot connect to a database.

5. Bulk Operations:
The MSDN documentation mentions that event handlers will not fire when bulk operation is occurring. For example, when a new list is created, the FieldAdding event will not fire. 


Limitations of Event Handler in SharePoint
Strangely there is no “SiteAdding”, “SiteAdded” event. However, if you really need this event you could create a “feature” to accomplish this. Features in SharePoint have a “FeatureActivated” event, where you could perform an action on creation of a site.


Summary 
Custom Event handlers are a powerful tool in your arsenal as a SharePoint Architect/ Developer!

Popular posts from this blog

How to deal with SharePoint 2010 exception "An exception occurred when trying to issue security token: The server was unable to process the request due to an internal error"

Scenario: You receive the below exception when you try to logon to a site that has been configured to use Claims Based Authentication with a custom membership provider using FBA credentials: Event ID from Event Log  - 8306 An exception occurred when trying to issue security token: The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.. Explanation: This error started to appear in our QA environment which does not have Visual Studio installed. I have tried starting the service "Claims to Windows Token Service" but that did not help either. I have made sure that all config...

Custom Form Base Login Web Part With Sharepoint 2010

In my  Last Post , I talked about creating a Form based authentication web application with Active Directory LDAP. After implementing that you have surely noticed that, when you try to sign in after creating a site collection from FBA web application, you get the following sign in page: Surely, it does not look pretty at all. So, you might want to change this default login page. In order to do this, first let us create an empty SharePoint project in Visual Studio 2010. After that, add an application page by right click-selecting “Add New Item” and you will get like the following window in Solution Explorer: Notice that, when you add any application page to your project, Visual Studio automatically creates a Layouts  folder. To keep things simple enough for your understanding, you may keep your application page under a folder where you might name it as same as your project like the figure above. Now, add a reference for  Microsoft.SharePoint.IdentityModel...

Migrating from Skype for Business to Microsoft Teams: A Step-by-Step Guide

Do you still use Skype for Business to meet the communication and collaboration needs of your business? If so, now is the perfect time to think about switching to Microsoft Teams, a cutting-edge platform with cutting-edge capabilities and seamless connectivity with other Microsoft services. But if you're unfamiliar with the procedure, switching to a new platform can seem like a difficult task. I'll walk you through the process of switching from Skype for Business to Microsoft Teams in this article. Plan the migration in Step 1 You must make a plan before you start the relocation procedure. Set a deadline, make a list of all the Skype for Business capabilities you presently use, and choose whether to migrate gradually or all at once. Step 2: Set up your surroundings. Make sure your network and infrastructure fulfil Microsoft Teams' standards. Upgrades to your hardware, software, and licences might be necessary. Additionally, confirm that you have the right permissions to ca...