Locked Add a event receiver handler to an specific list

Locked

  • Friday, March 20, 2009 1:55 PM
     
      Has Code
    How can I add a event receiver handler to an specific list?

    I saw that but I don't know how to use that:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/11c99348-bac2-43bf-b055-433f6eca5b16/

    Thanks


All Replies

  • Friday, March 20, 2009 2:10 PM
     
     
    You can find a full writeup on how to create features (and their callouts) here:

    The code that you found in the other forum post is indeed the way to go.


    Good luck!
    Jeroen

    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
  • Friday, March 20, 2009 2:26 PM
     
     Answered Has Code
    Below are the steps for attaching event receiver to particular list

    1. Create a class library which includes a one class say "featurereceiverclass" , that class should inherit    "SPFeatureReceiver" class

    2. After above step override FeatureActivated,FeatureInstalled,FeatureDeactivated,FeatureUninstalled events.

    3. In "featureactivated" method write the code which is taken from the post as mentioned by you

    public override void FeatureActivated(SPFeatureReceiverProperties properties)   
    {   
       SPWeb site = null;   
       try  
       {   
                        //Adds the overridden ItemUpdated Event Receiver to a list   
                        site = (SPWeb)properties.Feature.Parent;   
                        SPList lst = site.Lists["ListName"];   
                        string asmName = "AssemblyNameOfYourEventHandler, Version=1.0.0.0, Culture=neutral,  PublicKeyToken=EnterThePublicKeyTokenHere";   
                        string itemReceiverName = "Namespace.ClassOfEventHandler";   
                        lst.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, itemReceiverName);   
        } 
    }  
     
    lst.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, itemReceiverName);  

    Here whatever event you want to attach specify using this line
    SPEventReceiverType.ItemUpdated or SPEventReceiverType.ItemAdded or SPEventReceiverType.ItemUpdating or SPEventReceiverType.ItemAdding

    4. Now create a feature for attaching eventhandler

    5. Create a folder in feature's folder say "EventHandlerInstaller".

    6. Add a feature.xml file to it

    7. In feature.xml file specify "feature receiver assembly" and "feature receiver class". "feature receiver assembly" will the assembly of class "
    featurereceiverclass" as mentioned above and "feature receiver class" will be "yournamspace.featurereceiverclass"

    8. Now using installfeature command of stsadm install the feature and using activatefeature command of stsamd activate the feature on desired site.

    9. Hope these steps helps you . If you have any queries do revert back.
  • Friday, March 20, 2009 3:18 PM
     
      Has Code
    Don't forget to update your list.
    In this case:

    lst.Update() 

    The reply above works well, i did it also.
  • Friday, March 20, 2009 3:22 PM
     
     
    Yeah..thanx Thomas for pointing out
  • Friday, March 20, 2009 4:23 PM
     
     
    bahhh I got confused...

    I have created a EventHandler receiver and now this FeatureReceiver, which is the difference?

    Thanks
  • Friday, March 20, 2009 4:50 PM
     
     
    EventHandler receiver is used to catch the events of list and FeatureReceiver is used to catch the events of features. In order to attach event receiver to specific list you have to use feature . So when you activate feature the code written in featureactivated event is executed and event is attached to list. If you donot want to use feature you can use console application and write the code included in the method above in console application. But use of feature is a better way to that
  • Saturday, March 21, 2009 5:44 AM
     
      Has Code
    This is what I get.... It seems it doesn't recognize my snk. The .snk is in the GAC

    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsa 
    dm -o installfeature -filename EventHandlerInstaller\Feature.xml 
     
    Feature 'b901010a-caba-4366-9d7a-8449da200d3c' could not be installed because th 
    e loading of event receiver assembly "MyFeatureReceiver.snk" failed: System.IO.F 
    ileNotFoundException: Could not load file or assembly 'MyFeatureReceiver.snk' or 
     one of its dependencies. The system cannot find the file specified. 
    File name: 'MyFeatureReceiver.snk' 
       at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, 
    Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boo 
    lean throwOnFileNotFound, Boolean forIntrospection) 
       at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, E 
    vidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Bool 
    ean throwOnFileNotFound, Boolean forIntrospection) 
       at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence 
     assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) 
       at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence as 
    semblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) 
       at System.Reflection.Assembly.Load(String assemblyString) 
       at Microsoft.SharePoint.Administration.SPFeatureDefinition.get_ReceiverObject 
    () 
     
    WRN: Assembly binding logging is turned OFF. 
    To enable assembly bind failure logging, set the registry value [HKLM\Software\M 
    icrosoft\Fusion!EnableLog] (DWORD) to 1. 
    Note: There is some performance penalty associated with assembly bind failure lo 
    gging. 
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus 
    ion!EnableLog]. 
     


    My Feature.xml is like this:
     
    <?xml version="1.0" encoding="utf-8"?> 
    <Feature Id="B901010A-CABA-4366-9D7A-8449DA200D3C" 
    Title="FeatureWithReceiver" 
    Scope="Web" 
    Version="1.0.0.0" 
    Hidden="FALSE" 
    DefaultResourceFile="core" 
    xmlns="http://schemas.microsoft.com/sharepoint/" 
    ReceiverAssembly="MyFeatureReceiver.snk" 
    ReceiverClass="MyFeatureReceiver.MyFeatureReceiver" 
    > 
    <ElementManifests /> 
    </Feature> 

    Also another question:

    string asmName = "MyEventReceiver, Version=1.0.0.0, Culture=neutral,  PublicKeyToken=f3a349f2-449c-44fb-a4fa-bae32fba6822"
                     

    Where can I get that PublicKeyToken. Many GUI's have confused me.



  • Saturday, March 21, 2009 5:45 PM
     
     
    wiracocha:
    "In order to attach event receiver to specific list you have to use feature"
    This is not correct; you are free to use an EventReceiver without the FeatureReceiver!

    Ahmed Abuabdou SharePoint Developer Sure Technology & Consulting
  • Tuesday, August 17, 2010 5:48 PM
     
     
    how can I accomplish without using feature Receiver??
  • Tuesday, August 17, 2010 10:10 PM
     
     

    By writing a console application that has this:

    using (SPSite site = new SPSite(My_site_url_here))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            myList = web.Lists["My_list_name_here"];

                            SPEventReceiverDefinitionCollection receivers = myList.EventReceivers;
                            SPEventReceiverDefinition receiver = receivers.Add();
                            receiver.Name = "My_ItemAdded_Name_Here";  //Any name, just want a way to refer to it
                            receiver.Assembly = "MyAssemblyHere, Version=1.0.0.0, Culture=neutral, PublicKeyToken=My_assembly_GUID_here";
                            receiver.Class = "MyNamespace.MyClass";
                            receiver.Type = SPEventReceiverType.ItemAdded;  //or use whatever event you need here.
                            receiver.Update();

                        }

                     }

    KevinHou

     

  • Wednesday, August 18, 2010 5:02 PM
     
     

    Hi vikpri,

    I have already written steps to attach event receiver to list using feature

  • Tuesday, May 24, 2011 9:38 PM
     
     

    Don't Forget to add Security setting for your application console's code

    Add site.WebApplication.FormDigestSettings.Enabled = false;
    before  receiver.Update();

    For more about security issue while Attaching a EventHandler on a Sharepoint List, please visit this url: http://hussainnaqvi.blogspot.com/2011/05/security-validation-for-this-page-is.html