Exercise 1 – Add a Feature Receiver Class

Task 1 – Add a Feature Receiver Class

In this task, you will add a feature receiver class to your SharePoint project that will allow you to add custom code that will run when the feature is activated.

  1. Launch Visual Studio 2010 and select File >> New >> Project.
  2. In the New Project dialog, select SharePoint >> 2010 in the Installed Templates section and choose Empty SharePoint Project.
  3. Name the project CustomActions_EventReceivers, and save it in C:\%Office365TrainingKit%\Labs\2.3\Before.
  4. In the SharePoint Customization Wizard dialog, in the What local site do you want to use for debugging? textbox, enter the URL of the SharePoint on-premises site you created for this session, e.g. https://intranet.contoso.com/Lab02.
  5. Select the Deployas a sandboxed solution radio button.
  6. Right-click Features select Add Feature.
  7. Right-click Feature1.feature and select Add Event Receiver.

Task 2 – Populate the list

In this task, you will override the FeatureActivated method to insert data into an existing list. When the feature is activated, the code in the FeatureActivated method of the feature receiver will run. You will use this to add some items to a list.

  1. Replace the commented-out FeatureActivated method with the following code to get access to the Site context, retrieve a reference to the Products list via the SPList class and add items to the list:

    (Code snippet 2.3.1)

    C#

    public override void FeatureActivated(SPFeatureReceiverProperties properties) { using (SPWeb web = (SPWeb)properties.Feature.Parent) { SPList productsList = web.Lists["Products"]; SPListItem newItem1 = productsList.Items.Add(); newItem1["Title"] = "Destination Guide St. Johns"; newItem1["ProductDescription"] = "Travel guide for St. Johns, FL"; newItem1["ProductID"] = "1"; newItem1.Update(); SPListItem newItem2 = productsList.Items.Add(); newItem2["Title"] = "Do-it-Yourself Advanced Fireworks Set"; newItem2["ProductDescription"] = "Create your own fireworks indoors with this advanced fireworks set. Real gunpowder! Age 4-7."; newItem2["ProductID"] = "2"; newItem2.Update(); SPListItem newItem3 = productsList.Items.Add(); newItem3["Title"] = "Model Flying Fortress Airplane"; newItem3["ProductDescription"] = "Build a replica of a historic American bomber"; newItem3["ProductID"] = "3"; newItem3.Update(); } }
  2. Save your changes.

Task 3 – Test the Solution

In this task, you will deploy your solution to your SharePoint on-premises site and verify that the list is populated correctly.

  1. Press F5 to run the solution.
  2. Select the Products list from the quick launch.
  3. Verify that the following 3 entries have been added to the Products list.
  4. Close Internet Explorer to stop debugging.

Task 4 – Deploy the Solution to SharePoint Online

In this task, you will deploy the solution package to SharePoint online to confirm that it works as expected.

  1. In the Solution Explorer window, right-click on the CustomActions_EventReceivers project and select Package to package the project.
  2. Open Internet Explorer and navigate to your SharePoint Online site.
  3. Click Site Actions and then select Site Settings.

  4. Click the Solutions link in the Galleries section to view the site collection’s solution gallery.

  5. Click on the Solutions tab in the ribbon and click the Upload Solution button.

  6. Browse to the solution package at C:\ %Office365TrainingKit%\Labs\2.3\Source\Before\CustomActions_EventReceivers\bin\Debug\CustomActions_EventReceivers.wsp and click Open.
  7. In the Solution Gallery – Activate Solution, click the Activate button.
  8. Browse to the Lab02 site.
  9. Click Site Actions >> Site Settings and choose Manage Site Features.
  10. Click the Activate button next to the CustomActions_EventReceivers Feature1 feature to activate the feature and populate the Products list instance to the Lab02 sub site.
  11. Verify that the Products list appears in the Quick Launch.
  12. Open the Products list and verify that it is populated as expected.