Saturday, March 23, 2013

How to Create Event Receiver in SharePoint 2010


Hi friends,
                  This is very small post about Event receiver in SharePoint. When we create event receiver we have lot of options to select which type of event receiver we want to create. We can create event receiver for SharePoint site, List and libraries and workflow.  Here I am mostly focusing on different list template id and how can we trigger an event for a particular list and library.
To create event receiver we need to perform following steps:  
 è Select SharePoint Project template->2010->Event receiver. Type proper name for event receiver and select .net framework 3.5 because SharePoint 2010 is built on top off .NET framework 3.5.


     è Type your Site Url and select deploy as sandbox solution



 è Here we have different types of event receiver as shown bellow. We can create event receiver for List Events, List Item Events, List Email Events, Web Events and List workflow events.



 è If we want to create list type event receiver then we have many set of events related to SharePoint List. Bellow are list of events related to SharePoint list.

 è If you select”List Item Events” then you have another options available which shows about the event source. Below are many event source list such as custom list, announcement list, contact list etc..

 è If you select “list Email Events” then you can send mail to user when any operation is performed on list. Here also we have event source and some events related to source.
       
 è If you select “Web Event” type event then you have only event related to web.
      
 è Here we will create List Item events and we will chose Custom List as event receiver and select related events which needed.
          
è You can check that It shows ListTemplateId=”100” based on different event source SharePoint has fixed ListTemplateId for each.

   

 è Now suppose we want to execute this event receiver to a particular list which is type of custom list we can set ListUrl of that list as show below
      
 è As shown below this event receiver will execute only for list MyListName. Like this way we can execute event receiver for any document library and web site.

è If you changed name of your event receiver from solution explorer then you also need to change tags inside Element.xml file otherwise event will not fire for event receiver. So wherever EventReceiver1 exist in Element.xml change it with your new name.

Types of Event in Event Receiver:
 è Before Events (Synchronous): The before events are those that happen just after the event occurred, but before SharePoint writes any data to the SharePoint content database. Such events are useful for custom validation, rules checking, and cancellation of users’ actions.
 è After Events (Asynchronous): The After events are those that happen after SharePoint commits the data to the SharePoint content database. These “After” event handlers cannot cancel the current action/operation, but they are guaranteed to execute only after specific actions.

 Before events run in the same process and thread as the current action, while After events by default run in a background thread; however, you can force them to run synchronously, that means within the same process and thread of the current action.

 è Below list shows ListTemplateId for all SharePoint list and libraries.
     
ListTemplateId
Description
100
Generic list
101
Document library
102
Survey
103
Links List
104
Announcement list
105
Contact List
106
Event List
107
Task List
108
Discussion Board
109
Picture Library
110
Data Source
111
Site Template Gallery
112
User Information List
113
Web Part Gallery
114
List Template Gallery
115
Xml Form Library
116
Master page Gallery
117
No-Code Workflows
118
Custom Workflow Process
119
Wiki Page Library
120
Custom Grid for list
130
Data Connection Library
140
Workflow History
150
Gantt Tasks list
200
Meeting Series list
201
Meeting Agenda list
202
Meeting Attendees list
204
Meeting Decisions list
207
Meeting Objectives list
210
Meeting text box
211
Meeting Things To Bring list
212
Meeting Workspace Pages list
300
Portal Sites list
301
Blog Posts list
302
Blog Comments list
303
Blog Categories list
1100
Issue tracking
1200
Administrator tasks list
2002
Personal document library
2003
Private document library

è You can select or type any one of the template Id in your event receiver to work for template specific.

This post is also related to:
How to create list specific Event receiver in SharePoint
Different List Template Id in SharePoint 2010
SharePoint List , Document library and Internal Id 

Regards 
Gireesh Painuly

Happy Coding ... 

Sunday, February 3, 2013

Interface and Abstract Classes



What is an Interface? , What is an abstract class?
Difference between Interface and abstract class
When should we go for Interface and abstract class?

Hi Folks,

                   It’s sometimes very tough to decide when to use interface and abstract class. It may be software architecture or a developer who is involved in designing software architecture. I have gone through many interviews during my college days where I used to just remember what the basic different between both of them was!  But I was not aware of from where exactly some one needs to use interface and abstract classes while designing a software architecture.

Today I thought to help all those fresher of IT field to know what the Interface and abstract class means.

When should we use what? A fresher always worries about the programming languages. Most of them think that interface and abstract class may differ while implementing in different programming languages. Concept of interface and abstract class remain the same for all programming languages.

Note: I have used .net framework and C# as programming language.

Abstract Class: Abstract classes are one of the essential behaviors provided by object oriented programming languages. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
What Abstract Class can contain:
ð     Abstract class can have Fields
       eg: protected sting _strName;
ð     Abstract class can have properties
       eg: public string Name{get; set;}
ð     Abstract class can have methods
ð     Abstract class can have Constructor and destructor
ð     Abstract class can have Static methods
ð     Abstract class can have Indexer
ð     Abstract class can have either abstract method or not abstract methods
ð     Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
ð     Non abstract methods should have body in abstract class
ð     Methods in abstract class should only public or protected
ð     If we declare a method without an abstract keyword in abstract class; it shows an error and ask for the body of the method to be written in abstract class or either declare it as abstract method.
ð     If we declare an abstract method in abstract class it should be implemented in derived class otherwise program will display an error message while compiling.

What abstract class can’t have?
ð     Method in abstract class can not have private member
ð     Abstract method in base abstract class should not have body.
ð   Abstract methods can not be marked as Virtual because all abstract methods are by default virtual.
ð  Abstract method can not be static.
      ð  Abstract class can not be Static, Private, Sealed and Internal. Abstract class is by default public.


Interfaces:
Interface provides a way to achieve runtime polymorphism. Using interface we can invoke functions from different class

What interface contain:
ð     An interface can have methods signature
ð     Interface can have Delegate signature
ð     Interface can have Event signature
ð     Interface can have Properties signature.
ð     So a interface should look like below:

public interface Student
    {
        string FirstName
        {
            get;
            set;
        }
        void MyMessage();
        event EventHandler MyTest;
    }
What Interface can not have?
ð     Interface can not have constructor
ð     Interface can not have destructor
ð     Interface can not have static methods
ð     Interface can not have fields
ð     Interface methods can not have public or private accessibility modifier by default every thing is public inside interface.

When to use Abstract class:
ð     For instance, I am developing a solution for a college which has different branches and departments. All the branches and departments are looked over by the University. If any notice is issued by the University; it should be circulated to all the branches and departments. The notice may comprise of changes in fee structure or some departmental specific information.

Assume I have already a solution with following architecture:
public abstract class VTUUniversity
    {
        public string _strCollegeName;
        public string Name
        {
            get;
            set;
        }
       protected abstract void FeeStructure();
  
    }

  public class MVJCollege : VTUUniversity
    {
      
        protected override void FeeStructure()
        {
            Console.WriteLine("I am in MVJ College");
        }
    }

        public class RamayanCollege : VTUUniversity
       {
         protected override void FeeStructure()
         {
            Console.WriteLine(" I m in RamayaCollege");
         }
       }
ð     There are two colleges affiliated and branches of VTU University. Now suppose University does some changes in examination fee structure of MCA course and wants to inform all the branches to implement the fee structure; but on the contrary , Ramaya College does not have MCA course so they don’t need to implement or publish this notice on their board.

ð     So the new structure will look like this:

public abstract class VTUUniversity
    {
        public string _strCollegeName;
        public string Name
        {
            get;
            set;
        }
       protected abstract void FeeStructure();
       public virtual void MCADeptFeeStructure()
       {
        Console.WriteLine("MCA Depatment Has Increased Examination fee by: 2000");
        Console.ReadLine();
       }
    }

public class MVJCollege : VTUUniversity
    {
        protected override void FeeStructure()
        {
            Console.WriteLine("I am in MVJ College");
        }
        public override void MCADeptFeeStructure()
        {
            Console.WriteLine("I am in MVJ College Fee sructure");
            Console.ReadLine();
        }
    }

    public class RamayanCollege : VTUUniversity
    {
        protected override void FeeStructure()
        {
            Console.WriteLine(" I m in RamayaCollege");
        }
    }


  class Program
    {
        static void Main(string[] args)
        {
            MVJCollege objMvjc = new MVJCollege();
            RamayanCollege objRamaya = new RamayanCollege();
            objMvjc.MCADeptFeeStructure();
        }

    }

ð     So what we noticed here is, we have added a non abstract method on abstract class with body and it does not reflect any changes in RamayanCollege  class because they don’t have any related course and not need to implement this method. While MVJCollege class has implemented and did there customization on derived class.
ð     This says that we should use abstract class as a base class when we are developing a large application which is widely used by many clients. So if any changes are made in base class does not affect the existing client functionality.

When To Use Interface:

ð     In case of the situation mentioned above, if we would like to use a base class as in interface then we would need to implement MCADeptFeeStructure interface in all the branches of University. It does not matter weather the college has MCA course or it does not!

ð     Interface should be used if we are sure that there will not be any further changes in the base class in feature.
ð     If we want to implement multiple base class because we can inherit multiple base class if we use interface while this is not possible in case of abstract class.

Recommendation:
v      If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
v      If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
v      If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
v      If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Best Wishes..
Happy Coding..