Showing posts with label Event Receivers. Show all posts
Showing posts with label Event Receivers. Show all posts

Delete event receiver from a SharePoint list

In my previous post, we saw how we added an event receiver to a list. Now, we will see how to delete the existing event receiver on a list.
private void DeleteEventReceiverFromAList(string siteUrl)
{
using (SPSite site = new SPSite(siteUrl))
{
using(SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists["myList"];
if (list != null)
{
string className = "EventReceiverClass";
string asmName = "EventReceiverAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a865f0ecc234ea51";
web.AllowUnsafeUpdates = true;

int receivers = list.EventReceivers.Count;
bool isAddedReceiverExist = false;
bool isUpdatedReceiverExist = false;
for (int i = 0; i < receivers; i++)
{
SPEventReceiverDefinition eventReceiver = list.EventReceivers[i];
if (eventReceiver.Class == className && eventReceiver.Type == SPEventReceiverType.ItemAdded)
{
eventReceiver.Delete();
break;
}
}
}
}
catch { }
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}
In this code also, there is nothing to explain very detail. Please let me know if you have any questions.
Read More...

Add event receiver to a SharePoint list

This is very generic and everyone knows how to add an event receiver. But, usually we attach the event receiver on a list template, site etc. This post deals with adding event receiver to a specific list.
private void AddEventReceiverToAList(string siteUrl)
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists["myList"];
if (list != null)
{
int receivers = list.EventReceivers.Count;
string className = "EventReceiverClass";
string asmName = "EventReceiverAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a865f0ecc234ea51";
web.AllowUnsafeUpdates = true;
bool isAddedReceiverExist = false;
for (int i = 0; i < receivers; i++)
{
SPEventReceiverDefinition eventReceiver = list.EventReceivers[i];
if (eventReceiver.Class == className && eventReceiver.Type == SPEventReceiverType.ItemAdded)
{
isAddedReceiverExist = true;
break;
}
}
if (!isAddedReceiverExist)
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, className);
}
}
catch { }
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}
This is very straight forward code and hope you got it. Read More...

Check out causing the item update event receivers call twice in SharePoint

Event receivers will help us to write events to add custom logic to perform operations while adding/updating/deleting items in a list/libraries in SharePoint. We can register the receivers to all lists/libraries or to a particular list or a library. Here in this post I am explaining the special case of the item update event receivers.

This is regarding the calling of these events 2 times by SharePoint framework when check out action done on a document library item. When you enable the option Require Check Out in a document library and when you choose option to checkout a document in a document library then it will call the item update events twice [If you write any update event receivers on the list then only]. So take care to avoid the unwanted call, otherwise it will do some unwanted actions. Because of it there may be chances of performance issue. Below are the details of solving the issue.

If the vti_sourcecontrolcheckedoutby property exits in the "BeforeProperties" property but not in the "AfterProperties" property, the event was caused by checking in a document. So by using this logic we will put the logic in specific block.

if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
&& properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
    //This is when the update event is triggered by check-in.
}
else
{
    //This is triggered by events other than check-in action.
}

For more details:

http://support.microsoft.com/kb/939307

Read More...
Related Posts with Thumbnails
GiF Pictures, Images and Photos