locked
Activate the feature, and also bind the eventhandler to a particular list in the site.

    Question

  • Hi,
        
       I have written the event handler for the Task List in sharepoint on my local machine, now i need to deploy it on the remote sharepoint server.

    I have used the WSP builder in my project, and created the deployment folder. By using the generated .bat file I'm able to deploy the eventhandler as feature on the sharepoint server.

    Now i need to activate the feature, and also bind the eventhandler to a particular list in the site.

    The requirement is to create a deployment package like stuff  which can deploy, activate the feature and bind the event handler.

    Can anyone please suggest me how to do it ?
    Biju Software Engineer,MCP
    Wednesday, July 29, 2009 3:32 AM

Answers

  • The links that I gave you pretty much cover everything, but here it is:

    1. In VS.NET create an SharePonint WSP project

    At this point a project will be created with the following structure

    Project
       12

    You need to add couple of more subfolders. At the end the project will look like this:

    Project
        -12
            -TEMPLATE
                -FEATURES
                    - TaskListEventHandler /* not mandatory but good practice. I've put the name of your event receiver, but you can put anything you want */
                        - feature.xml
     BindEventReceiver.cs
     TaskListEventHandler.cs
        
    2. Add a class to your project that inherits the SPItemEventReceiver in order to handle the ItemAdding, ItemUpdating, ItemDeleting, etc. event - you already have this one.

    3. Add a class to your project that inherits the SPFeatureReceiver, so you can handle the FeatureActivated and FeatureDeactivated where you will bind/unbing the event receiver that you have created. You can add this class to the project where your list even handler is.

    public class BindEventReceiver : SPFeatureReceiver
    {
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
              SPList customList  = web.Lists["ProjectStatus"]; 
             if (customList != null)
             {               //TODO: change the assembly name and the class with the ones you have. 
                   string assemblyName = "{fully qualified assembly name}";
                   string className = Test.SPTaskListEventHandler";
    
                   customList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
                   customList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assemblyName, className);
                   customList.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, className);
             }
        }
    }
    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
             //TODO: Remove the event receivers in a similar way they were added. 
        }
    }
    
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {       }
    
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {        }
    
    }
    
    


    4. Add a feature.xml to the feature folder (shown above)

    <Feature Id="5E88AE4E-2ECB-4CC5-B9EC-9019250F5BC8"
      Title="Task List Event Handler "
      Scope="Site"
      ReceiverAssembly="{fully qualified name of your assembly here}"
    ReceiverClass="BindEventReceiver" 
      xmlns="http://schemas.microsoft.com/sharepoint/">



    At this point you can compile the WSP and you will have everything that is needed inside.

    5. deploy the wsp and activate the feature

    You already have the command for deploying the feature so what you need to add is the activation one:

    stsadm -o activatefeature -name TaskListEventHandler -url http://TestingSite


    Hope I didn't miss somethig. Note that I used some example names/namespaces which you can replace with your real ones.

    Regards,
    Pako


    BLOG: iTechnology Blog
    • Marked as answer by B1ju Wednesday, July 29, 2009 9:31 AM
    • Unmarked as answer by B1ju Wednesday, July 29, 2009 11:04 AM
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 5:43 AM
  • Hi Pako,

     Thankyou very much for your solution.  The solution works great!!!!


     I needed one more suggestion from you, in the code we can see that the list name "ProjectStatus" is hardcoded. Can we make the code a bit generic in such a way that the list name should be provided as an argument (not hardcoded) ?

    Please suggest......
    Biju Software Engineer,MCP
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 9:31 AM
  • You can just add one more element to the feature.xml:

    <feature ......>
    ......................
    <Properties>
        <Property
          Key="ListName"
          Value="ProjectStatus"/>
    </Properties>
    </feature>

    Then in the feature receiver where I've specified the list name you can use the following:

    web.Lists[properties.Feature.Properties["ListName"]]

    Of course you can take care of the situation where there is no such property or the list is missing. The above code just shows you the idea.

    Regards,
    Pako
    BLOG: iTechnology Blog
    • Edited by Pako Simeonov Wednesday, July 29, 2009 3:10 PM
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 3:08 PM

  •  But i couldn't makeout where to place the code? and how to use it?


    As I feel that this code needs to be running as soon as the solution is deployed on the site (ie., before binding the "Task List Event Handler" feature



    You can try to put it in the FeatureInstalled method of the BindEventReceiver  class. This way when the feature is installed you will activate from there which will cause the FeatureActivated method to be called where you will bind the event receiver to the list.

    Regards,
    Pako
    BLOG: iTechnology Blog
    • Marked as answer by B1ju Friday, July 31, 2009 4:53 AM
    Thursday, July 30, 2009 4:34 PM
  • Hi Pako,

      I tried the code  in the featureinstalled event, and it works fine now
    SPWeb web = new SPSite("http://yourserver.com").OpenWeb();
    
    SPFeatureCollection featureCollect = web.Features;
    
    featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true); //True to force activation

    Thankyou Pako.......
    Biju Software Engineer,MCP
    • Marked as answer by B1ju Friday, July 31, 2009 4:52 AM
    Friday, July 31, 2009 4:51 AM

All replies

  • Hi,

    You can bind the even handler using the feature/elements xml only to a list template : http://developers.de/blogs/adis_jugo/archive/2009/03/12/develop-and-deploy-a-sharepoint-event-receiver-from-the-scratch.aspx . If you want to bind it to a specific list then you will need to write some code:

    using (SPSite site = new SPSite("http://{siteurl}"))
    {
        using (SPWeb web = site.OpenWeb())
       {
            SPList customList  = web.Lists["{List Name}"]; 
    
            string assemblyName = "TaskListReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dd4d54c05b75eae9";
    string className = "{NameSpace}.{TaskListRecieverClass}";
    
            //TODO: add all the even reciever types. Here is an example for ItemAdding
            customList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
        }
    }
    You can do that also using a feature receiver which you can specify for the feature you have. You can override the FeatureActivated method of the feature receiver and put the code in there. If the list to attach the feature to is in the same site where the feature is being activated you can direcly use the properties.Feature.Parent as SPWeb . Here is one example that I found: http://blogs.msdn.com/hanz/archive/2009/02/06/how-to-create-a-feature-receiver-that-update-a-theme.aspx

    Regards,
    Pako
    BLOG: iTechnology Blog
    Wednesday, July 29, 2009 4:12 AM
  • Hi Pako,
        Could you please tell me where exactly should i put the code which you have sent? and how to use it?

    I will try to explain the exact scenario by point by point.

    1)I created an event handler (inheriting the SPItemEventReceiver for the events like ItemAdding,ItemDeleting andItemUpdating), I deployed it manually,activated the feature using a thirdparty software (.exe downloaded from the net)   called the "Event handler Explorer". Now the event handler works fine.

    2) Now to deliver it to the customer, i need to give it to him as a deployment package, so i used the WSP builder (downloaded from the net). Using the WSP builder i get a deployment folder which contains the .wsp and .bat files. By running the .bat file i tested it by executing it on the server, it deploys the feature sucessfully. The .bat file has the following commands

    stsadm -o retractsolution -name "Test.SPTaskListEventHandler.wsp" -immediate
    stsadm -o execadmsvcjobs
    stsadm -o deletesolution -name "Test.SPTaskListEventHandler.wsp"
    stsadm -o execadmsvcjobs
    stsadm -o addsolution -filename "Test.SPTaskListEventHandler.wsp"
    stsadm -o execadmsvcjobs
    stsadm -o deploysolution -name "Test.SPTaskListEventHandler.wsp" -immediate -allowGacDeployment -allowCasPolicies -force
    stsadm -o execadmsvcjobs

     3) Now i need to Activate the feature on the site, and next bind the event handler to a task list called "ProjectStatus" in a site called "TestingSite". I tested it by manually going into the site features and activating it, later bind the events to the  "ProjectStatus" list using the "Event handler Explorer", and the events gets triggered sucessfully.

    4) Now, my requirement is to automize the process of  activating the feature and binding the events to the list.

     Please suggest me the steps or some way by which i can achieve this .


    Biju Software Engineer,MCP
    Wednesday, July 29, 2009 4:54 AM
  • The links that I gave you pretty much cover everything, but here it is:

    1. In VS.NET create an SharePonint WSP project

    At this point a project will be created with the following structure

    Project
       12

    You need to add couple of more subfolders. At the end the project will look like this:

    Project
        -12
            -TEMPLATE
                -FEATURES
                    - TaskListEventHandler /* not mandatory but good practice. I've put the name of your event receiver, but you can put anything you want */
                        - feature.xml
     BindEventReceiver.cs
     TaskListEventHandler.cs
        
    2. Add a class to your project that inherits the SPItemEventReceiver in order to handle the ItemAdding, ItemUpdating, ItemDeleting, etc. event - you already have this one.

    3. Add a class to your project that inherits the SPFeatureReceiver, so you can handle the FeatureActivated and FeatureDeactivated where you will bind/unbing the event receiver that you have created. You can add this class to the project where your list even handler is.

    public class BindEventReceiver : SPFeatureReceiver
    {
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
              SPList customList  = web.Lists["ProjectStatus"]; 
             if (customList != null)
             {               //TODO: change the assembly name and the class with the ones you have. 
                   string assemblyName = "{fully qualified assembly name}";
                   string className = Test.SPTaskListEventHandler";
    
                   customList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
                   customList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assemblyName, className);
                   customList.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, className);
             }
        }
    }
    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
             //TODO: Remove the event receivers in a similar way they were added. 
        }
    }
    
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {       }
    
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {        }
    
    }
    
    


    4. Add a feature.xml to the feature folder (shown above)

    <Feature Id="5E88AE4E-2ECB-4CC5-B9EC-9019250F5BC8"
      Title="Task List Event Handler "
      Scope="Site"
      ReceiverAssembly="{fully qualified name of your assembly here}"
    ReceiverClass="BindEventReceiver" 
      xmlns="http://schemas.microsoft.com/sharepoint/">



    At this point you can compile the WSP and you will have everything that is needed inside.

    5. deploy the wsp and activate the feature

    You already have the command for deploying the feature so what you need to add is the activation one:

    stsadm -o activatefeature -name TaskListEventHandler -url http://TestingSite


    Hope I didn't miss somethig. Note that I used some example names/namespaces which you can replace with your real ones.

    Regards,
    Pako


    BLOG: iTechnology Blog
    • Marked as answer by B1ju Wednesday, July 29, 2009 9:31 AM
    • Unmarked as answer by B1ju Wednesday, July 29, 2009 11:04 AM
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 5:43 AM
  • Hi Pako,

     Thankyou very much for your solution.  The solution works great!!!!


     I needed one more suggestion from you, in the code we can see that the list name "ProjectStatus" is hardcoded. Can we make the code a bit generic in such a way that the list name should be provided as an argument (not hardcoded) ?

    Please suggest......
    Biju Software Engineer,MCP
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 9:31 AM
  • You can just add one more element to the feature.xml:

    <feature ......>
    ......................
    <Properties>
        <Property
          Key="ListName"
          Value="ProjectStatus"/>
    </Properties>
    </feature>

    Then in the feature receiver where I've specified the list name you can use the following:

    web.Lists[properties.Feature.Properties["ListName"]]

    Of course you can take care of the situation where there is no such property or the list is missing. The above code just shows you the idea.

    Regards,
    Pako
    BLOG: iTechnology Blog
    • Edited by Pako Simeonov Wednesday, July 29, 2009 3:10 PM
    • Marked as answer by Chengyi Wu Thursday, July 30, 2009 3:48 AM
    Wednesday, July 29, 2009 3:08 PM
  • Hi Pako,

     thanks again, for your reply :) .
    Just for my information, I wanted to know that can we activate a deployed feature through code (C#), If yes, then could you please give some information/steps to do do it?

    With regards
    Biju
    Biju Software Engineer,MCP
    Thursday, July 30, 2009 7:37 AM
  • Hi Pako,
      I checked the link,  the code is pretty straight forward

    SPWeb web = new SPSite("http://yourserver.com").OpenWeb();
    
    SPFeatureCollection featureCollect = web.Features;
    
    featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true); //True to force activation
    


    But i couldn't makeout where to place the code? and how to use it?


    As I feel that this code needs to be running as soon as the solution is deployed on the site (ie., before binding the "Task List Event Handler" feature

    But i couldn't makeout where to place the code? and how to use it? Could you please guide me with this?

    Biju Software Engineer,MCP
    Thursday, July 30, 2009 8:57 AM
  • Hi Pako,

       Currently, after deploying the feature i could find the feature in the SiteFeatur. The feature is not in a active state.  I tried to add stssdm -o Activatefeature command in the batch file but still it is not getting activated.




    The batch file which am using (generated through the WSPBuilder deploy folder) is as follows

    REM WSPBuilder generated commands

    stsadm -o retractsolution -name "Test.SPTaskListEventHandler.wsp" -immediate stsadm -o execadmsvcjobs stsadm -o deletesolution -name "Test.SPTaskListEventHandler.wsp" stsadm -o execadmsvcjobs stsadm -o addsolution -filename "Test.SPTaskListEventHandler.wsp" stsadm -o execadmsvcjobs stsadm -o deploysolution -name "Test.SPTaskListEventHandler.wsp" -immediate -allowGacDeployment -allowCasPolicies -force stsadm -o execadmsvcjobs REM Activate feature command-- added by me stsadm -o activatefeature -filename CustomizedItemEventReceiver\Feature.xml -url http://testsharepoint.com/sites/projectcenter/ echo "Feature Activated" iisreset pause
    Can you please suggest me how to make the feature "Active" while deploying the feature?



    Biju Software Engineer,MCP
    Thursday, July 30, 2009 11:45 AM

  •  But i couldn't makeout where to place the code? and how to use it?


    As I feel that this code needs to be running as soon as the solution is deployed on the site (ie., before binding the "Task List Event Handler" feature



    You can try to put it in the FeatureInstalled method of the BindEventReceiver  class. This way when the feature is installed you will activate from there which will cause the FeatureActivated method to be called where you will bind the event receiver to the list.

    Regards,
    Pako
    BLOG: iTechnology Blog
    • Marked as answer by B1ju Friday, July 31, 2009 4:53 AM
    Thursday, July 30, 2009 4:34 PM
  • Do you get an error while executing the stsadm -o activatefeature command?

    BLOG: iTechnology Blog
    Thursday, July 30, 2009 4:38 PM
  • Hi Pako,

       When i run the stsadm -o activatefeature command I dont get any error. 
    Biju Software Engineer,MCP
    Friday, July 31, 2009 2:50 AM
  • Hi Pako,

      I tried the code  in the featureinstalled event, and it works fine now
    SPWeb web = new SPSite("http://yourserver.com").OpenWeb();
    
    SPFeatureCollection featureCollect = web.Features;
    
    featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true); //True to force activation

    Thankyou Pako.......
    Biju Software Engineer,MCP
    • Marked as answer by B1ju Friday, July 31, 2009 4:52 AM
    Friday, July 31, 2009 4:51 AM