Sharepoint Event receivers

on Saturday, August 29, 2009

Example 1:Updating Item properties

Imp Note: See the use of EnableEventFiring() and DisableEventFiring() methods in ItemUpdated event to prvent infinite loop of event firing.
public class CustomListItemReceiver:SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
File.AppendAllText(@"c:\abc.txt", properties.ListTitle);
}
catch (Exception exception)
{
throw;
}
}

public override void ItemDeleting(SPItemEventProperties properties)
{
base.ItemDeleting(properties);
}

public override void ItemUpdated(SPItemEventProperties properties)
{
DisableEventFiring();
properties.ListItem["Nickname"] = properties.ListItem["Nickname"] + "hello";
properties.ListItem.Update();
EnableEventFiring();
File.AppendAllText(@"c:\abc.txt", properties.ListTitle);
}

Example 2: Invoke SPD workflow from event receivers

public class InvokeWorkflow : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem currentItem = properties.ListItem;
SPWorkflowManager manager = currentItem.Web.Site.WorkflowManager;
foreach (SPWorkflowAssociation association in currentItem.ParentList.WorkflowAssociations)
{

manager.StartWorkflow(properties.ListItem, association,"", false);
}
}
}

Event Registration>


Method 1:Using Object Model to attach receiver to specific doc lib

using (SPSite site = new SPSite("http://localhost:1982/"))
{
using (SPWeb web = site.OpenWeb())
{
//File.AppendAllText("c:\\abc.txt", "hello");
SPList list = web.Lists["Leave Applications"];
foreach (SPEventReceiverDefinition def in list.EventReceivers)
{
//if(def.Class.Contains("ListItemEvent"))
//def.Delete();
Console.WriteLine(def.Name + def.Type.ToString() + def.Class);
}
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "FormLibraryEventListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f46df2818ed96ab", "FormLibraryEventListener.InvokeWorkflow");
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, "FormLibraryEventListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f46df2818ed96ab", "FormLibraryEventListener.InvokeWorkflow");
}
}

Method 2:Using Features to attach receiver to specific Template doclibs

Feature.xml file content
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="1E90FEC8-0301-4e77-A261-2907FF5BDECF"
         Title="Invoke Workflow Event Receiver" Scope="Web">
  <ElementManifests>
    <ElementManifest Location ="elements.xml" />
  </ElementManifests>
</Feature> 
elements.xml file content
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId ="115">
    <Receiver>
      <Name>MyListItemEventReceiver</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>FormLibraryEventListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f46df2818ed96ab</Assembly>
      <Class>FormLibraryEventListener.InvokeWorkflow</Class>
    </Receiver>
    <Receiver>
      <Name>MyListItemEventReceiver</Name>
      <Type>ItemUpdated</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>FormLibraryEventListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f46df2818ed96ab</Assembly>
      <Class>FormLibraryEventListener.InvokeWorkflow</Class>
    </Receiver>

  </Receivers>
</Elements>






0 comments: