Tuesday, August 26, 2014

Attach event receiver to specific list/lists

SharePoint 2010 Event Receiver: -Attach event receiver to specific lists.
............................................................
Creating Event Receiver:
Open visual studio2013 --- > Click on new project ----- >
give name: SampleEvent& Location: C:\solutions\sampleEvent---- >
next Click on Ok button.--- > Click on solution explorer
next give siteurl and select deploy as
a farm solution click on finish. Next right click on solution -- >add --- >
NewItem -- >select SharePoint-- >2010
-- >select event receiver give name:sampleEvent --- >Add
Select event receiver type as List item events from dropdown,
Event source as Customlist from dropdown, check required event
(ex: An Item is being added) next click on finish button. Write required code in Receiver1.cs file
Ex:
namespace EventReciever.EventReceiver11
{
///

/// List Item Events
///
public class EventReceiver11 : SPItemEventReceiver
{
///

/// An item is being added.
///
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
GetValues(properties);
}
void GetValues(SPItemEventProperties properties)
{
try
{
// Get a reference to the current site.
SPWeb currentWeb = properties.OpenWeb();
// Get a reference to the list.
SPList lstAstatus = currentWeb.Lists["AlgorithmStatus"];
SPListItem ListItem = lstAstatus.Items.Add();
ListItem["LoginName"] = currentWeb.CurrentUser.Name;
ListItem["Task"] = "AlgorithmCalculation";
ListItem["Status"] = "C";
this.EventFiringEnabled = false;
ListItem.Update();
this.EventFiringEnabled = true;
}
catch
{
}
}
}
}
- In Solution Explorer open “Elements.xml”
To use an event receiver for one specific list:
- Replace the attribute “ListTemplateId” with “ListUrl” and add your url as value:
------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 
 <Receivers  ListTemplateID="100">
      <Receiver>
        <Name>LogHistoryItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Axalta.Global.DpciformsLogHistory.LogHistory.LogHistory</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>

</Elements>
--------------------------------------------------------------------------------------
-> Replace With:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
 <Receivers  ListUrl="Lists/NotesCda">
      <Receiver>
        <Name>LogHistoryItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Axalta.Global.DpciformsLogHistory.LogHistory.LogHistory</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>
… Tip:
To use an event receiver for two or more lists:
You have to add one more “&ltReceivers&gt” node
Sample:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
 <Receivers  ListUrl="Lists/List1">
      <Receiver>
        <Name>LogHistoryItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Axalta.Global.DpciformsLogHistory.LogHistory.LogHistory</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>

 <Receivers  ListUrl="Lists/List2">
      <Receiver>
        <Name>LogHistoryItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Axalta.Global.DpciformsLogHistory.LogHistory.LogHistory</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

No comments:

Post a Comment