SharePoint User Group UK

Share the knowledge!

Welcome to SharePoint User Group UK Sign in | Join | Help
in
Home Blogs Forums Photos Files Roller

Registering an Event Receiver to a Specific List or Content Type

Last post 11-26-2008, 7:38 PM by macraem. 12 replies.
Sort Posts: Previous Next
  •  11-21-2008, 2:01 PM 15460

    Registering an Event Receiver to a Specific List or Content Type

    Guys

    I cannot find any decent documentation anywhere on this. 

    I wish to register my Event Receiver to a specific Content Type, or to a specific List within a workspace.  Can you provide me with a method to do this? In particular the elements.xml I need to build for feature deployment.

    Alternatively I have heard you could use a Feature Receiver to deploy to a specific list, but I am unsure again how this can be achieved.  Also would this method propogate if the subsite was saved as a template?

    Thanks

    Mark


    If it bleeds, we can kill it.
  •  11-21-2008, 3:30 PM 15464 in reply to 15460

    Re: Registering an Event Receiver to a Specific List or Content Type

    hi,

    I registered a feature event receiver for my feature in the feature.xml using the "ReceiverAssembly" and "ReceiverCalss" attributes of the Feature tag,

    -----
    <Feature Id="EE2458B8-4594-4bef-A9F9-29D9F3D3A632" Title="Whatever" Description="blah blah blah." Version="1.0.0.0" Scope="Web" Hidden="FALSE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/" ReceiverAssembly="Assembly.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXX" ReceiverClass="Event.Receiver.Class.Name" ImageUrl="smiley_face.gif">
    -----

    and then in that class, I bound an event handler to an instance of a list (installed by the feature) in code:

    -----
    string assm = "Assembly.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx";
    string nsp = "Assembly.Class.NameSpace";

    SPList targList = web.Lists["TargetList"];

    targList.EventReceivers.Add(SPEventReceiverType.EmailReceived, assm, nsp);
    -----

    imagine you could do the same thing with a content type in code



    Cheers,

    Nigel Witherdin
  •  11-22-2008, 6:23 PM 15480 in reply to 15464

    Re: Registering an Event Receiver to a Specific List or Content Type

    Nice,exactly what I was after

    Thanks


    If it bleeds, we can kill it.
  •  11-24-2008, 10:16 AM 15490 in reply to 15480

    Re: Registering an Event Receiver to a Specific List or Content Type

    Exactly the same method I use.. from what I've found out, this is the only way to do it for a specific list.. Content types and List types can however be done through the feature framework.

    The Pattison Larson book "Inside Microsoft Windows Sharepoint Services 3.0" has a good article on this.

    Paul.


    www.myfatblog.co.uk
    MCTS: WSS 3.0
    MCTS: MOSS 2007
  •  11-24-2008, 10:25 AM 15494 in reply to 15490

    Re: Registering an Event Receiver to a Specific List or Content Type

    Hi Mark,

    Have a look at http://www.davehunter.co.uk/Blog/Lists/Posts/Post.aspx?ID=69.

    You can bind an event receiver to a list using xml, but this is on list type and not instance (needs to be done through code - spfeaturereceiver).  Similarly if you are using a custom content type you have defined yourself you can bind the event receiver to this (see article for example).  If its an out of the box content type you need to do this through code using a feature receiver.

    Hope this helps

    Dave


    My SharePoint Blog www.davehunter.co.uk/blog
  •  11-25-2008, 9:56 AM 15557 in reply to 15494

    Re: Registering an Event Receiver to a Specific List or Content Type

    Guys

    Thanks for your advice and help, however I still have a problem. 

    When using a Feature Receiver, would you still define an Elements.Xml for your Feature?  Im having trouble getting my head round this.  If I define Elements.Xml then I have to specify a ListTemplateId, which if I set to "100", will install the feature for all custom lists within the site I activate on??

    Would one of you kindly include an example full Feature Xml where a Feature Receiver is used?

    Many thanks

    Mark


    If it bleeds, we can kill it.
  •  11-25-2008, 10:04 AM 15560 in reply to 15557

    Re: Registering an Event Receiver to a Specific List or Content Type

    Hi Mark,

    If you only want to bind the event receiver to one instance of a list then you don't need a feature.  The event receiver assembly needs to be placed in the gac.  You then need to write a SPFeatureReciever that gets the instance of the list and adds event receiver by using the SPList.EventReceivers.Add().

    Hope this helps

    Dave


    My SharePoint Blog www.davehunter.co.uk/blog
  •  11-25-2008, 10:07 AM 15561 in reply to 15560

    Re: Registering an Event Receiver to a Specific List or Content Type

    You could look at the event receiver management http://chrissyblanco.blogspot.com/2007/08/event-receiver-management.html.  This would give you a visual understanding of adding event receivers to a list, before you get lost in the coding side of things.
    My SharePoint Blog www.davehunter.co.uk/blog
  •  11-25-2008, 1:10 PM 15574 in reply to 15560

    Re: Registering an Event Receiver to a Specific List or Content Type

    Dave

    Thanks but I must be having a dense moment cos I still dont quite understand.  If you dont write a feature, how will you get the SpFeatureReceiver to fire?

    My method so far has been:

    1. Write my EventReceiver and store in GAC

    2. Write my FeatureReceiver which (onFeatureActivated) will append my EventReceiver to a ContentType/Specific List's EventReceivers collection, and add this to the GAC

    3. Deploy a Feature (with no Elements Manifest) but specify the ReceiverAssembly and ReceiverClass attributes (pointing at my Feature Receiver)

    4. Activate the Feature, which fires my FeatureReceiver code and attaches my EventReceiver to the specific List/ContentType

    This is all fine, but Im wondering if Im missing something or making life more complicated that need be.

    With regards to your comment "If you only want to bind the event receiver to one instance of a list then you don't need a feature" can you explain this a little more? Thanks for your time and help!

    Cheers

    Mark

     

     


    If it bleeds, we can kill it.
  •  11-25-2008, 1:53 PM 15577 in reply to 15574

    Re: Registering an Event Receiver to a Specific List or Content Type

    Hi Mark,

    I meant you didn't need a feature file or elements for the SP Event Receiver.  This is fired after you add it through the EventReceivers collection of a SPList.

    With regards to the the other comment.  Say you wanted to implement logging when a user adds a new document to a document library.  Instead of adding the event receiver to each list you can associate the event receiver to the document library type using the feature file.

    If you want to bind the event receiver to say a document library called "publications", you will do this through code using a SPFeatureReceiver.

    It sounds like you are implementing the correct method.

    To summarise you have two components:

    1. Event Receiver.  This is a class that responds to events before or after events like ItemAdded or ItemAdding.  To attach this business rule / functionality to a list you will need to associate it through an XML file (applies to all lists of a certain type) or

    2. SPFeatureReceiver.  Programatically add a event receiver to the SPList.EventReceivers collection.

    Hope this helps

    Dave


    My SharePoint Blog www.davehunter.co.uk/blog
  •  11-25-2008, 2:18 PM 15580 in reply to 15577

    Re: Registering an Event Receiver to a Specific List or Content Type

    Thanks Dave, all clear now :)

    Whilst on the subject though, do you have any preference on how to use configuration with an Event Receiver?  e.g. rather than hard code Field Names etc.  Also the same goes for a FeatureReceiver I guess.

    Web.Config?

    Thanks a lot

    Mark


    If it bleeds, we can kill it.
  •  11-25-2008, 2:45 PM 15585 in reply to 15580

    Re: Registering an Event Receiver to a Specific List or Content Type

    Hi Mark,

    Configuration for a event receiver is simple.  You can use the

    <properties>

    <property Key="ListName" Value="Publications" />

    </properties>  for more info http://msdn.microsoft.com/en-us/library/ms475601.aspx

    Configuration for the event receiver is a little tricky, the event receiver doesn't have a feature xml so you can't store it there.  If you feel that the information can change and should be freely updatable you can store it in a list in the root web site.  Then create a helper class to retrieve these configuration values.

    Hope this helps

    Dave


    My SharePoint Blog www.davehunter.co.uk/blog
  •  11-26-2008, 7:38 PM 15635 in reply to 15585

    Re: Registering an Event Receiver to a Specific List or Content Type

    Just some feedback on this, I successfully deployed my Event Handler to a Content Type using the following method:

    - Built my EventHandler to do the required job on ItemAdded, and deployed to the GAC

    - Created a Feature, and in the Xml referenced a Feature Receiver assembly using the ReceiverAssembly/ReceiverClass.  I also used the Properties nodes to hold details of the ContentType I wanted to attach the eventhandler to and also the details of the EventHandler itself (assembly, type).

    - In my FeatureReceiver assembly, I overrode the FeatureActivated and FeatureDeactivating methods to Add/Delete the EventHandler to the ContentType.  Also, handy to do a contentType.Update(true) here, so that the EventHandler gets added/removed to all the child/list instances also.

    Now I can control the EventHandler from one place - feature in the topsite!

    Thanks for all your help - so much appreciated!

    Mark


    If it bleeds, we can kill it.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems