Sunday, June 24, 2012

Event Reciver SharePoint 2010


Remember that the suffix "-ing" indicates that the event is being handled before the action occurs, and the suffix "-ed" indicates that the event is being handled after the action occurs.

Cancel deletion of item :

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace MyEventHandlers
{
    public class SimpleEventHandler : SPItemEventReceiver    {
    }
}
public override void ItemDeleting(SPItemEventProperties properties)
{
    properties.Cancel = true;
    properties.ErrorMessage = "Deleting is not supported.";
}

Adding of  item


public override void ItemAdded(SPItemEventProperties properties)
{
    SPListItem oItem = properties.ListItem;
    oItem["Body"] = "Body text maintained by the system.";
    oItem.Update();
}

---------- Complete example  ----


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
namespace ERDefinition
{
    public class ItemEvents : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPListItem item = properties.ListItem;
            item["Title"] = item["Title"] + " - " + DateTime.Now;
            item.Update();
        }
    }
}

 

    --------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace BindItemEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Shared Documents"];

                    SPEventReceiverDefinition def = list.EventReceivers.Add();

                    def.Assembly = "ERDefinition, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=704f58d28567dc00";
                    def.Class = "ERDefinition.ItemEvents";
                    def.Name = "ItemAdded Event";
                    def.Type = SPEventReceiverType.ItemAdded;
                    def.SequenceNumber = 1000;
                    def.Synchronization = SPEventReceiverSynchronization.Synchronous;
                    def.Update();
                }
            }
        }
    }
}

 

 

Site Events

Web Events

List Events

List Field Events

Item Events

No comments:

Post a Comment