Reputation: 325
I've wrote an event receiver with the purpose of changing all documents containing the "+" symbol to "plus":
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace Plus_Sign.EventReciever1
{
public class EventReceiver1 : SPItemEventReciever
{
private string fileName;
///An item is being added
publc override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SPListItem item = properties.ListItem;
fileName = item["Name"}.ToString();
if (fileName.Contains("+"))
{
fileName.Replace("+", "plus");
};
}
}
}
What do I need here to make it bind to my list? Or the whole Site? I've tried researching it and I wasn't able to find a straight answer, or at least one I could understand. I asked a SharePoint developer that I know in passing and he mentioned that I would need some code to add it and he recommended that I use SharePoint manager but I could not figure it out. What do I need and how do I find it?
Upvotes: 0
Views: 146
Reputation: 889
The registration of event receiver is not defined within the event receiver itself. It's done in elements.xml file. Here is the sample:
<Receivers ListTemplateId="107">
<Receiver>
<Name>EventReceiver1ItemAdding</Name>
<Type>ItemAdding</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>VisualWebPartProject1.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receirvers>
The key is in Receivers element. ListTemplateId allows you to specify that these event receivers will be registered to list based on specified list template id.
You may also use ListUrl attribute to specify particular list by its URL.
Beware of one particular strange behaviour of SharePoint event receiver declarative registration (elements.xml). If the scope of feature that the event receiver is part of is Site Collection your event receiver will be registered on all (even system) lists within that site collection. Sometimes it might be useful but mostly it is not.
You may also register event receiver by code in feature receiver (FeatureActivated event). But you should also handle FeatureDeactivating event to deregister your event receiver.
To check proper registration you may use tools like SharePoint Manager or SharePoint event receiver manager.
Upvotes: 1
Reputation: 556
Ok, when you created this project in VIS2010, you must have selected the List type, I believe your list is Custom List( for custom list it is ListTemplateId="100") . So it must have got bound to all the custom list, now if you want to run this only for particular list, than you will have to check the title of list.
if (properties.ListTitle == "<List Tile>"){
SPListItem item = properties.ListItem;
fileName = item["Name"}.ToString();
if (fileName.Contains("+"))
{
fileName.Replace("+", "plus");
};
}
Upvotes: 2
Reputation: 437
Go to the feature which contains your event receiver under your subsite, even if it's activated then deactivating and reactivating that feature should attach that event receiver to your List/Library
Upvotes: -1