Skip to main content

Patrick Tisseghem's Blog [MVP SharePoint]

Go Search
U2U Blog Center
U2U website
  

The Happy SharePoint Traveller

U2U Blog Center > Patrick Tisseghem's Blog [MVP SharePoint] > Posts > Free Tool: Register Event Handlers Programmatically (WSS 2007)
Free Tool: Register Event Handlers Programmatically (WSS 2007)

In WSS 2007, event handlers have been improved a lot and will allow you to extend your SharePoint environment in a lot of ways. We have synchronous before (means you can now cancel a user action) and asynchronous after events. Events are fired not only in document libraries but now also for lists - and even at various other levels (site and web level). It is also now possible to hook up multiple event handler assemblies to your list or library you work with. During my session at the SharePoint Advisor Summit in Vegas I demonstrated a small Windows application I am using to register an event handler for a list. And I also promised to make it available for download. Here is the link to it. Remember that this is demoware and meant to be something to learn from (that's why you get the full source code) and it was created for beta 1. So if you are not into the beta program, wait a couple of weeks and then you will be able to download beta 2 and I will make sure that I have by that time tested and recompiled it for that version.

Here are kind of the demo steps I used:

  1. Use VS.NET 2005 to create your event handler. It can be as simple as this:

    using Microsoft.SharePoint;

    public class MyEventHandler: SPItemEventReceiver
    {
         public override void ItemDeleting(SPItemEventProperties properties)
         {
               properties.Cancel = true;
               properties.ErrorMessage = "Sorry, we do not allow deleting of items...";
         }
    }
  2. Next, sign your assembly and deploy it into the GAC.
  3. Open the EventHandler Explorer tool
  4. Type in the URL to your SharePoint site
  5. Select the list/library/content type/web you want to hook up with.


  6. Load the assembly.
  7. Select the class, enter the sequence number and the type of event
  8. Click register.
  9. Double-click the node in the treeview to verify your work.

Comments

CoreResource exception

I keep getting the following error when I click explore:

"The type initializer for 'Microsoft.SharePoint.CoreResource' threw an exception"

Any idea what that means?
at 9/03/2007 18:24

news blog

Hey man, thanks for article, but I still have recurrent problems with explore your site in Internet Explorer 4.0. please fix this problem. it's not very convinient to browse your site if there are no design :(

http://ireadnews.net/
[URL=http://ireadnews.net]News Blog[/URL]
<a href="http://ireadnews.net">News Blog</a>
at 19/04/2007 10:29

Great tool but...

Great tool but does not seem to work for events such as ItemAttachmentAdded, ItemAttachmentAdding...cause the events never fires later on in MOSS and/or WSS

Any idea?

Thanks
at 8/06/2007 17:43

The type initializer for "Microsoft.SharePoint.CoreResource" threw an exception.

I tried compiling and giving the url of the site collection. it gives the following error. Any help on this?

The type initializer for "Microsoft.SharePoint.CoreResource" threw an exception.

Ram
parshuram.chenna@i3intl.com
at 4/07/2007 8:54

Thanks

Thanks alot
at 25/07/2007 21:00

The type initializer for "Microsoft.SharePoint.CoreResource" threw an exception.

Same problem occured, how to fix it?

The type initializer for "Microsoft.SharePoint.CoreResource" threw an exception.

Thanks
Bob
at 26/07/2007 4:06

ItemAdded Event is not attaching properly

Hi,

  it's an excellent tool., i'm trying to attach evenhandler for ItemAdded(Seq 1001). It's not attaching. I'd appreciate if you can help me regard this.
at 1/08/2007 19:04

ItemAdded Event is not attaching properly

Hi,

  it's an excellent tool., i'm trying to attach evenhandler for ItemAdded(Seq 1001). It's not attaching. I'd appreciate if you can help me regard this.
at 1/08/2007 19:04

Fix

If you have problems with this item
(see post of: The type initializer for "Microsoft.SharePoint.CoreResource" threw an exception.)

Make sure you run the TOOL (and every sharepoint-code) on the SHAREPOINT-SERVER (and not on you clientmachine)
at 6/08/2007 11:17

Its not working

I tried with ItemCheckedOut event but is not working. The event handler is listed like in the picture above, the Assembly is loaded etc...

CounterHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9dcf06a69d3d219

Sequence : 10000
what can I do?
please help!!!
at 13/09/2007 15:53

Acesss Denied

I downloaded the tool and ran it when i try to explore the site it gives an error message "Access Denied".

Help?
at 17/09/2007 13:13

thiet ke web, dich thuat, thiet ke poster, thiet ke website, quang cao in an, xay dung thuong hieu

thiet ke web, thiet ke logo, thiet ke brochure, thiet ke website, thiet ke in an, web, website, lap trinh web, cho thue may chu, cung cap email, mail server, dich thuat, thiet ke website, quang cao in an, xay dung thuong hieu

481/581/9 Le Quang Dinh street,wart 11, Dist : Binh thanh ,Ho Chi Minh city
Unit: 603 - Fl 6, 87 Tran Thien Chanh Str, Dist 10, Ho Chi Minh city
Tel: 848 5158499
848 2978144
848 5158500
fax:  8482978143
at 18/09/2007 9:29

Test

Test
at 19/09/2007 10:27

where is the link to download

download link
at 2/10/2007 9:38

Link to download

at 3/10/2007 13:18

Greatwork !! Thanks a bunch !!!!

This is a cool tool.. Makes my day much better..

Thanks a lot !
at 10/10/2007 23:25

Remove Eventhandlers Problem

Hi,

first of all - great tool, a lot of thanks
i found a bug at removing a eventhandler - there is no update call on the parent-object

i fixed it with the following lines:

 er.Delete();
                    if (node.Parent.Parent.Tag is SPContentType)
                        ((SPContentType)node.Parent.Parent.Tag).Update();
                    if (node.Parent.Parent.Tag is SPWeb)
                        ((SPWeb)node.Parent.Parent.Tag).Update();
                    if (node.Parent.Parent.Tag is SPList)
                        ((SPList)node.Parent.Parent.Tag).Update();

                    textBoxAssemlby.Text = string.Empty;


maybe this will help other people
at 5/11/2007 11:45

assembly does not load

when I try and do step 6 the assembly is not loaded. The dialogue box that allows me to select the .dll just flashes when I select the .dll. How can I get this to work?
at 22/11/2007 0:04

ACCESS DENIED

If you are getting an Access Denied message after clicking the "Explore" button then you are probably not running the RegisterEventHandler app in the context of a Site Collection Administrator account.  Go to Central Administration -> Application Management -> Site Collection Administrators and add yourself as the Secondary site collection administrator.  Be sure that you add yourself to the correct site collection.
at 28/11/2007 2:52

Error loading assembly

I copied the code above and went through the steps but when I tried to load the assembly I get the following error message:

"Could not load file or assembly 'MyEventHandler, Version=1.0.0.0,....' or one of its dependencies.  The system cannot find the file specified."

Could you please help?

Thank you.
at 11/12/2007 18:19

ItemAdded not getting attached but ItemDeleting works!

First i was having test event handler for ItemDeleting. I was able to execute it as well as debug it. But now when i changed the event handler to ItemAdded from ItemDeleting i can see that it is not getting fired at all.

Anything i am missing. Please RE: back on viraj.deshpande@gmail.com
at 28/12/2007 20:53

How to : Add dynamic MOSS Event to every new created site??

1. Your Artical is good.
2. I want to register the event on the document Library. but the functionalty is such that I will be creating site every week and the same document library will be used. that mean there is dynamic site creation and dynamic library creation. then how i can register the MOSS event dynamically??????
In short:  How to add MOSS Events  dynamically when my site is getting creating everytime.
clue: i am also using site template (Is it possiable to add moss event to site template, so that every time when i create site using these template i get moss event attached.) Or is there any other way out.
Thanks
Labhesh Shrimali
at 2/01/2008 15:02

URL

I can't seem to connect to our server.  (Web application at .... could not be found ...) What should be the format of the URL ??  Could it be that I need a password when I goto the site using IE?

Machine name:  machine
Site name: site
at 9/01/2008 23:18

Item View Event, not available :( - Patrick got any idea how to...

I am working to generate a dynamic list with most popular links under list view. I need to capture view item event to log its entry in database so that I could show popular items.

Is it possible through other way, I have been checking item auditing but doesn't appeal me much.

Any idea will highly appreciated.

Majid
at 1/02/2008 16:39

Thanks

Thanks for the tool!!!
at 15/03/2008 12:54

Content Type fix

Hey - Love the app, thanks!!! However, I was having a problem getting content type event handlers to attach properly - it appeared they were attached but they didn't fire properly.  Also, I was unable to remove them properly.  Again, it visually appeared to work in the app, but if i reloaded the site the event handlers came back.  After doing some browsing, it seems a few other people experienced this issue as well.

I added to following code and it seems to be working properly now:
snippet 1, within buttonRemove_click immediately after er.Delete();:
string tag = treeViewItems.SelectedNode.Parent.Tag.ToString();
if (tag.ToLower().Equals("contenttype event handlers")){
   SPContentType item = (SPContentType)treeViewItems.SelectedNode.Parent.Parent.Tag;
   item.Update(true);
}

snippet 2, within buttonAdd_Click - within the if(node.Tag is SPContentType) block, change ct.Update() to ct.Update(true)

I'm working off of a MOSS 2007 sp1 virtual, and i'm not sure if this problem is unique to only me and a few others or not - unfortunately i'm still not completely understanding of the logic behind the event handler registration process - but i hope this helps anyone else having the same problem as me.
at 13/06/2008 15:35

Awesome app! Thanks a lot

I really love it and it made my work a lot easier (even though MOSS didn't work as expected). Thanks from Argentina.
at 18/07/2008 3:15

Thank you

thanks for the tool
it really is a charm.
:)
at 24/11/2008 7:50

Please help

I love this interface, however I'm running into a slight problem. When I'm at the Select .NET Assembly selection window, if I double click or try to hit "open" the dll, nothing happens. In fact, it's almost like the Open button is a reset button. I can't load the assembly at all.

Any suggestions would be greatly appreciated.

Thank you
at 24/11/2008 21:28

This Tool is AWESOME!

I had some problems getting it to work with Content Types, but thanks to the visitors before me, have been able to get it working!  (Thanks to all contributors for the code snippets!) HUGE!
at 31/03/2009 20:46

Delete an event handler

How do you delete an eventhandler?
at 22/06/2009 15:46
Captcha

Enter the code shown above: *

(Note: If you cannot read the numbers in the above
image, reload the page to generate a new one.)