Creating an Event Receiver in SharePoint

Creating an event receiver in SharePoint helps the site to respond and interact with items such as lists or list-items. You can change the code of the event receiver to update changes made on the calendar or when a name is deleted from the contact list. Event receivers in SharePoint helps to keep a site well updated on all changes made to a list.

If you know how to use CSOM code in SharePoint, you can easily manage and create list items of your site. In order to add an event receiver to a site, you need to install visual basic studio, a SharePoint version and all other windows supported editions.

Creating an Event Receiver in SharePoint

How to add an event receiver in SharePoint list instance

  • Open your project that you have created, the project can be a site column, list or content type.
  • On the solution explorer, choose a project node that is named “Clinic”
  • Choose project and then “Add-New-item” on the menu bar.
  • Under either visual basic or visual C#, expand the “SharePoint-Node” and then choose 2010 0r 2013 depending on your sharepoint version.
  •  Choose “Event-receiver” from the templates pane and name it “Test-Event-Receiver1”. Click OK to continue.
  •  When the “SharePoint-Customization-Wizard” pops up choose “List-Items-Events”
  • when requested to choose the item that should be in the event source, choose ” Patients-{Clinic\Patients}
  •  Check the check-box next to the item added and then click on the finish button to complete.
  • In the next step you will be required to add your code that will be renamed as Scott-brown by default.
  • After you have completed adding your codes, fill all the data fields and click on the save button.

Creating event receivers in SharePoint requires one to learn some basic knowledge on visual basic and visual C# languanges. If you don’t know how to use visual C# or basic studio, you can seek help from an experienced programmer to help you create an event receiver. After you have created an event receiver, you can run it to test how it works, use the following steps to test and run an event handler debugger.

How to test and run an event handler debugger:

  • Open your event receiver and press F5 on your keyboard to run the project.
  • When a dialog box appears to notify you that the “remote_Debugging” is not enabled. click on the OK button option to continue.
  • In the “Solution_Explorer” choose a sharepoint project.
  • Choose “Project-Properties” in the menu bar to view the “project-property” page. Select and tick on the “Remote-Event-Debugging” check box located at the bottom of the “SharePoint-Properties-Page”
  • Paste the connection string of your “Azure_Service_Bus” and in it’s appropriate box and then save your project.
  • Open the event receiver file named “Remote_Event_Receiver1.SVC” and add a break point at any line of code contained in that file.
  • Click on F5 to run the project again.
  • If you want to add a new item to the list choose the “Add-New-Item” button option.
  • Name the item and click on the save button.
  • Click on F5 again to run the project. If all the changes are effective save the project.

In older versions of SharePoint, an event receiver was used to handle events such as deleting and creating lists, with SharePoint 2013, you can use a remote event receiver to handle all events in your site. A remote event receiver works in the same way as a regular event receiver. The only difference between the two receivers is that a remote event receiver is used to handle all events that occur that occur when a sharepoint app is on a different system of it;s web host application.

If you want to handle events that occur on items of a sharepoint app, use the following steps

How to handle all events that occur to items of a SharePoint App

  • Use Visual studio to open the “Solution_Explorer” in your computer. Choose the project-node for the SharePoint app.
  • Choose the project and navigate to “Add-new-item” on the menu bar.
  • Choose remote-event-receiver in the templates-pane. Specify a suitable name and then click on the Add button.
  • The “SharePoint-Customization-Wizard” will appear.
  • Choose an event receiver type on the “Choose-event-receiver” settings page.
  • Set the ‘event-source’ to a list of the events that you would like to handle.
  • In the lists-of-events-to-handle, check the box next to each an every event that you would like to handle and then click on the finish button option to continue.
  • A web app is created with the web service that contain the code file.
  • Add a code to the code file in order for it to execute functions that you specify in your event receiver.

After you have done the following procedure, you will be able to easily customize your app event receiver on SharePoint. All list items are given a code that performs a particular function when you specify a path in your SharePoint app.

How to remove or add event handlers in SharePoint apps

  •  Open the solution explorer and choose the project-node of the remote-event-receiver.
  •  Press on F4 key on your keyboard to view the properties window or use the menu bar located at the top of your screen.
  • When promoted to the “properties-Windows”, set the properties for all events that you want to handle to the “True” mode.
  • If you don’t want to set a property value set that property to “False”
  • Add the “event handling-code” to the remote receiver of the sharepoint app.

If you want to handle another type of a different event, add another remote-event-receiver to the sharepoint app. for example, if the remote-receiver handles “List-item-events” add another event to it.

Before you embark on creating app event receivers, you should first familiarize yourself with how to create sharepoint web apps and how to integrate them with your site content. If you are new to SharePoint apps and event receiver, you should learn the basic concepts on how they operate and function.

Beside learning all these information, you will also need to have some fundamental concepts of how to handle event receivers in sharepoint. If you follow the above illustrated steps and procedures, you will find it easy to create event receivers for your sharepoint sites. Creating  an event receiver in SharePoint helps to arrange your lists and items in an orderly manner that keeps your site running in a professional way.

mm
Marissa Hart is the Lead Author & Editor ShareMe. ShareMe is a blog focused on SharePoint Online. SharePoint Online delivers the powerful features of SharePoint without the associated overhead of managing the infrastructure.