none
how to add a Event Handling in Windows SharePoint Services RRS feed

  • Question

  • Hi all,

       how to add a Event Handling In Share Point  for particular document library or list.

        actually i saw one article for restrict the deletion  i tried that article,

       i created one class library in that i added one class and two xml files(elements.xml and    feature.xml).

    Code are give Below.

     

    class file

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace EventHandlerExample1
    {
      public class CancelAnnouncementDeleteHandler : SPItemEventReceiver
      {
        public override void ItemDeleting(SPItemEventProperties properties)
        {
          string HandledMessage = ("Announcements can not be deleted from this list");
          properties.ErrorMessage = HandledMessage;
          properties.Cancel = true;
    
        }
    
    
      }
    }
    

     

    Elements.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <ListTemplate 
       Name="announce" 
       Type="104" 
       BaseType="0" 
       OnQuickLaunch="TRUE" 
       SecurityBits="11" 
       Sequence="320"
       DisplayName="$Resources:core,announceList;" 
       Description="$Resources:core,announceList_Desc;" 
       Image="/_layouts/images/itann.gif"/>
     <Receivers ListTemplateId="104">
      <Receiver>
       <Name>EventHandlerExample1</Name>
       <Type>ItemDeleting</Type>
       <SequenceNumber>20000</SequenceNumber>
       <Assembly>
        EventHandlerExample1, Version=1.0.0.0,
        culture=neutral, PublicKeyToken=23d13acd76dea7ec
       </Assembly>
       <Class>EventHandlerExample1.CancelAnnouncementDeleteHandler</Class>
       <Data></Data>
       <Filter></Filter>
      </Receiver>
     </Receivers>
    
    </Elements>
    

    features.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Feature Id="A1C2EBE3-2994-478b-9375-9665E7004D11" 
      Title="$Resources:core,announcementslistFeatureTitle;"
      Description="$Resources:core,announcementslistFeatureDesc;"
      Version="1.0.0.0"
      Scope="Web"  
      xmlns="http://schemas.microsoft.com/sharepoint/">
     <ElementManifests>
      <ElementManifest Location="Elements.xml"/>
     </ElementManifests>
    </Feature>
    

    i save all in one project. after that i create one console application in that i added that event handlers to particular library. here the code. 

     

     using (SPSite site = new SPSite("SPSITE URL"))
          {
            using (SPWeb web = site.OpenWeb())
            {
    
              SPList myList = web.Lists["Announcements"];
    
              SPEventReceiverDefinitionCollection receivers = myList.EventReceivers;
              SPEventReceiverDefinition receiver = receivers.Add();
              receiver.Name = "EventHandlerExample1"; //Any name, just want a way to refer to it
              receiver.Assembly = "EventHandlerExample1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=23d13acd76dea7ec";
              receiver.Class = "EventHandlerExample1.CancelAnnouncementDeleteHandler";
              receiver.Type = SPEventReceiverType.ItemAdded; //or use whatever event you need here.
              receiver.Update();
    
            }

     

               while i build this i didn't get any error. i copied the web application dll in corresponding Share Point Server too. after that also deletion is occurred in announcement. can anyone tell me how to restrict a deletion for particular list or library.

     

    Thanks In Advance,

    Regards,

    Saravanan Ashokan

     

    Tuesday, November 23, 2010 2:31 PM

Answers

  • Hi,

    In fact that you didn't need any console application to deploy the handler if you used the feature file to deploy the handler.

    You can create a WSP project and add a new item. From the WSP template you could select the event handler with feature template.

    Then the WSP tool would add an event class , feature.xml and element.xml file to your project automatically.

    You just need to develop the event class and specify the ListTemplateId to the list template that you want to use the handler to process.

    e.g.

    Elements.xml

    ?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <Receivers ListTemplateId="101">
     <Receiver>
      <Name>ItemHandler</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>RegisterItemAddingEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b9b4a243b9906e6e</Assembly>
      <Class>RegisterItemAddingEvent.ShareDocHandler</Class>
      <Data></Data>
      <Filter></Filter>
     </Receiver>
     <Receiver>
      <Name>ItemHandler</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10001</SequenceNumber>
      <Assembly>RegisterItemAddingEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b9b4a243b9906e6e</Assembly>
      <Class>RegisterItemAddingEvent.ShareDocHandler</Class>
      <Data></Data>
      <Filter></Filter>
     </Receiver>
     </Receivers>
    </Elements>

     

     Or you can develop an event handler by inherited from the SPItemEventReceiver and deploy it to GAC.

    After that you can create a console application to add the handler for one specific list.

    http://farhanfaiz.wordpress.com/2007/12/31/creating-adding-an-event-handler/


    Microsoft Online Community Support
    Thursday, November 25, 2010 3:02 AM

All replies

  • Hi,

    In fact that you didn't need any console application to deploy the handler if you used the feature file to deploy the handler.

    You can create a WSP project and add a new item. From the WSP template you could select the event handler with feature template.

    Then the WSP tool would add an event class , feature.xml and element.xml file to your project automatically.

    You just need to develop the event class and specify the ListTemplateId to the list template that you want to use the handler to process.

    e.g.

    Elements.xml

    ?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <Receivers ListTemplateId="101">
     <Receiver>
      <Name>ItemHandler</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>RegisterItemAddingEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b9b4a243b9906e6e</Assembly>
      <Class>RegisterItemAddingEvent.ShareDocHandler</Class>
      <Data></Data>
      <Filter></Filter>
     </Receiver>
     <Receiver>
      <Name>ItemHandler</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10001</SequenceNumber>
      <Assembly>RegisterItemAddingEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b9b4a243b9906e6e</Assembly>
      <Class>RegisterItemAddingEvent.ShareDocHandler</Class>
      <Data></Data>
      <Filter></Filter>
     </Receiver>
     </Receivers>
    </Elements>

     

     Or you can develop an event handler by inherited from the SPItemEventReceiver and deploy it to GAC.

    After that you can create a console application to add the handler for one specific list.

    http://farhanfaiz.wordpress.com/2007/12/31/creating-adding-an-event-handler/


    Microsoft Online Community Support
    Thursday, November 25, 2010 3:02 AM
  • You are linking the event receiver to the itemadded event :

    receiver.Type = SPEventReceiverType.ItemAdded; //or use whatever event you need here.
     


    Best regards, Mahmoud Challouf MCTS .NET Framework , WCF , SharePoint www.innersharepoint.blogspot.com
    Friday, November 26, 2010 11:31 PM