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

Friday, December 9, 2011

Blocking updates for document library

Scenario:
For one of the new requirement we needed to block all updates to documents in document library

1. User should able to upload a new document
2. User should not be able to upload a document with the same file name as existing document
3. User should not be able to edit metadata once it has been entered.

Solution:
Simple event receiver did the trick but as always with some learning / gotta's.

Assuming 2 different situations
1. Simple document library with no extra metadata columns : In this we will need ItemAdded event to update the event once.

2. Document library with extra properties : We dont need ItemAdded event and code can be commented.

Code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SKN
{
class EventHandler1 : SPItemEventReceiver
{
public override void ItemAdded (SPItemEventProperties properties)
{
base.ItemAdded(properties);

//DisableEventFiring();
//properties.ListItem.Update();
//EnableEventFiring();
}

public override void ItemUpdating (SPItemEventProperties properties)
{
base.ItemUpdating(properties);

if (!string.Equals(properties.ListItem["Created"], properties.ListItem["Modified"]))
{
properties.Cancel = true;
properties.ErrorMessage = "no update";
}
}
}
}

Friday, May 15, 2009

Event Handler exceptional behaviour

Scenario:
Programming events can cause exceptional behaviors, depending on the contexts in which you implement the event handlers. The following table describes event behaviors related to specific contexts that you might encounter when writing code.

Few known behaviour:

1. A request to delete an item through a list form does not have an associated content type ID, but this causes an ItemDeleting or ItemDeleted event to fire on all items, not just on items of a specific content type, even though the event was registered only for the content type.

2. The BeforeProperties property applies only to DocumentLibrary type lists.

3. List events do not fire on the UserInfo list.

4. Event handlers will not fire when bulk operation is occurring. For example, when a new list is created, the FieldAdding event will not fire.

5. Using element file to register an event handler attaches it to all the list of particular type. Most of the requirements need event handler to be attached to a particular list which can only be done through code.

Article:
MSDN

Event Handlers- Everything you need to know

Saturday, September 6, 2008

Creating a basic Item Event Handler

Scenario:
You want to write an event handler to cancel the deletion of an item from Announcement List ( Type = 104 )

Event Handler Code:


using Microsoft.SharePoint;

namespace TrainingSamples
{
public class DeletingAction : SPItemEventReceiver{
public override void ItemDeleting(SPItemEventProperties properties){
properties.Cancel = true;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}
}
}
Feature.xml

<Feature Scope="Web"
Title="Deleting Event Handler"
Id="GUID"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
</Feature>
Elements.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="104">
<Receiver>
<Name>DeletingEventHandler</Name>
<Type>ItemDeleting</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a26b5449ac4a4cf3</Assembly>
<Class>DeletingEventHandler.DeletingAction</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>


Additional Notes:
This will attach event handler to any list of Type=104. So If you have specific requirements to attach event handler to a particular list only, then go for a Feature Receiver.

Tuesday, August 5, 2008

Registering list item Event Handler

Scenario:
You want to add an event handler from a particular item within your list/library

Pre-requisite:
You already have assembly compiled

Code:

using Microsoft.SharePoint ;

SPSite site = new SPSite("http://moss");
SPWeb web = site.AllWebs["/"];
SPList list = web.Lists["List_Name"];

string assemblyName = "[full 4-part assembly name]";
string className = "Namespace.ClassName";

list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, className);
Use:
Console Application, At the time of Feature Activation

Deleting all the registered event handlers from a list

Scenario:
You want to deleting all the registered event handler from a particular list/library

Code:

using Microsoft.SharePoint ;

SPSite site = new SPSite("http://mossserver");
SPWeb web = site.AllWebs["/"];
SPList list = web.Lists["List_Name"];

int itemCount = list.EventReceivers.Count;

for (int k = itemCount-1 ; k >= 0 ; k--)
{
list.EventReceivers[k].Delete();
}
Use:
Console Application, At the time of Feature De-activation