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

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

How to: Debug SharePoint Applications

You can greatly simplify debugging by using Visual Studio extensions for Windows SharePoint Services. This topic includes procedures that explain the following: Debugging with Visual Studio extensions for Windows SharePoint Services Performing manual debugging Performing remote debugging Debugging with Visual Studio extensions for Windows SharePoint Services Press the F5 key to begin debugging with Visual Studio extensions for Windows SharePoint Services. The following procedure demonstrates how to enable F5 debugging. To enable F5 debugging Locate and open the target SharePoint application's Web.config file. By default, it is located in C:\Inetpub\wwwroot\wss\VirtualDirectories\80. Find the following line of code and change the  debug  attribute to  true . Save the changes to the Web.config file. In Visual Studio, right-click the SharePoint project, and then click  Properties . Click the  Debug  tab, and then type the target SharePoint URL in t

SharePoint 2010 – How to use Audio and Video Webpart

Video and Audio Web Part [ a.k.a Media Web Part ] is one of the new cool web parts in SharePoint 2010, so in this article I am going to show you the way to add this web part in your page. To add Media web part to the page you must activate those two features 1- SharePoint Server Publishing Infrastructure Feature in Site Collection features 2- SharePoint Server Publishing Feature in Site features After that edit page then click on Insert tab in SharePoint Ribbon then select Video and Audio Now the Media Web Part in the page Click on the Media Web Part and you will find Options Tab in the Ribbon As you can see in the Options tab there are Chang Media button allow you to select the video and Chang Image button to change the image of web part and Styles (Dark, Light) Click on Change Media to select the Media file. From Computer opens up the Upload media so you can upload the video and display it. Once you click ok and saving the page. Click on play button and enj