Thursday, April 14, 2011

SharePoint 2010 List Events Enhancements

Have you ever worked with SharePoint 2007 List Item Events? If yes then you would have probably noticed that when we attach any event receiver with list then it gets attached at the template level
what I mean is suppose I have created an event receiver for say Calculations List which is basically a custom list (template id = 100) , so in SharePoint 2007 all events will be called for all lists which are created by using  custom list template id = 100
and workaround was that , we were taking help of code to avoid this in SP 2007 , like we can check the name of list every time event receiver gets called and make execution only for desired list
Sample Code:
public override void ItemAdded(SPItemEventProperties properties)
{
  if (properties.ListTitle.Equals("YourList"))
  {
     //here you go          
  }
}
Now to avoid this in SharePoint 2010, framework comes with some enhancements done in Event Receivers section
Now we can associate particular event receiver at Site / Web / List levels and so this is more granular now
 Basically there are three more attributes are added for <Receivers> element and those are
Scope: we can define the scope of Event Receiver to SiteCollection (Site) or Web level
RootWebOnly: event receiver will be attached to all lists under root web created using particular template
ListUrl: we can specify particular list on which Event Receiver will be active (/Lists/MyList/)
Example:
<Receivers ListTemplateId="100" ListUrl="/Lists/CustomList/">
    <Receiver>
      <Name></Name>
      <Type></Type>
      <Assembly></Assembly>
      <Class></Class>
      <SequenceNumber></SequenceNumber>
    </Receiver>
 </Receivers>

No comments:

Post a Comment