Thursday, September 6, 2012

Attach Event Receiver to Specific List Programmatically in SharePoint 2010

Recently, developed a Event Receiver to set Item Level permissions based on the list field "Visible to Visitors". Event receiver binds with all document libraries using ListTemplateId element in Elements.xml.  Event Receiver to Set Item Level Permissions based on List Column Value

How to Associate Event Receiver with a List Programmatically in SharePoint 2010:
SharePoint 2010 offers ListURL to be specified in Elements.xml file to attach the Event Handler to the particular list. MOSS 2007 doesn't! So you have to write code to Register, May be Console application/Feature Receiver, etc.

Code to Register Event Receiver with a Particular List:

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

namespace SetItemLevelPermission
{ 
    //Class to Register Event Receiver to particular List

    class BindEventHandlerToLists : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
              SPWeb  web  =  (SPWeb)properties.Feature.Parent;

              //Get the Lists in which the Event receiver to be attached. We can Get this from Config file instead of hard coding
              //Check whether the List exists!
              SPList list = web.Lists["Project Documents"];

              //Set the Assembly, Class names
              string AssemblyName = "SetItemLevelPermission, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e166fe20cd991fe";
              string ClassName = "SetItemLevelPermission.SetPermissions";

              //Register the event handlers
              list.EventReceivers.Add(SPEventReceiverType.ItemAdded, AssemblyName, ClassName);
              list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, AssemblyName, ClassName);
              
            list.Update();

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            SPList list = web.Lists["Project Documents"];

            // Remove the Binding of the Event Handler from Lists
            string AssemblyName = "SetItemLevelPermission, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e166fe20cd991fe";

            //Delete the Event Receivers
            for(int i=list.EventReceivers.Count-1; i>=0; i--)
            {
                if (list.EventReceivers[i].Assembly == AssemblyName)
                {
                    list.EventReceivers[i].Delete();
                }
            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
 }



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...