1 / 19

Module 5 Creating Event Receivers and Application Settings

Module 5 Creating Event Receivers and Application Settings. Module Overview. Creating Event Receivers Working with Web.Config Programmatically Lab: Creating Event Receivers and Web.Config Modifications. Lesson 1: Creating Event Receivers. Types of Event Targets Types of Events

petere
Download Presentation

Module 5 Creating Event Receivers and Application Settings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 5 Creating Event Receivers and Application Settings

  2. Module Overview • Creating Event Receivers • Working with Web.Config Programmatically • Lab: Creating Event Receivers and Web.Config Modifications

  3. Lesson 1: Creating Event Receivers • Types of Event Targets • Types of Events • Creating List Item Event Receivers • Creating List E-Mail Event Receivers • Creating List Workflow Event Receivers • Creating List Event Receivers • Creating Web Event Receivers • Creating Feature Receivers • Canceling Events

  4. Types of Event Targets • List items • List e-mails • List workflows • Lists • Webs • Features

  5. Types of Events • Before events (synchronous) • After events (asynchronous)

  6. Creating List Item Event Receivers • SPItemEventReceiver class • Overriding events • ItemAdding, ItemAdded, ItemDeleting, ItemDeleted • ItemUpdating, ItemUpdated • ItemAttachmentAdding, ItemAttachmentAdded, ItemAttachementDeleting, ItemAttachementDeleted • ItemCheckingIn, ItemCheckedIn, ItemCheckingOut, ItemCheckedOut, ItemUncheckingOut, ItemUncheckedOut • ItemFileMoving, ItemFileMoved, ItemFileConverted • SPItemEventProperties object public class CheckJobs : SPItemEventReceiver { public override void ItemUpdating(SPItemEventProperties properties) { string oldJobTitle = properties.BeforeProperties["Title"].ToString(); string newJobTitle = properties.AfterProperties["Title"].ToString(); ... base.ItemUpdating(properties); } }

  7. Creating List E-Mail Event Receivers • SPEmailEventReceiver class • Overriding events • SPEmailEventReceiver • SPList Property • SPEmailMessage property public class CatchEmails : SPEmailEventReceiver { public override void EmailReceived( SPList list, SPEmailMessageemailMessage, String receiverData) { string listTitle = list.Title; string sender = emailMessage.Sender; string body = emailMessage.PlainTextBody; ... base.EmailReceived(list, emailMessage, receiverData); } }

  8. Creating List Workflow Event Receivers • SPWorkflowEventReceiver class • Overriding events • WorkflowStarting • WorkflowStarted • WorkflowCompleted • SPWorkflowEventProperties • Workflow Initiation forms public class MyListWorkflows : SPWorkflowEventReceiver { public override void WorkflowStarted(SPWorkflowEventProperties properties) { string myInitData = properties.InitiationData; ... base.WorkflowStarted(properties); } }

  9. Creating List Event Receivers • SPListEventReceiver class • Overriding events • FieldAdding, FieldAdded • FieldDeleting, FieldDeleted • FieldUpdating, FieldUpdated • ListAdding, ListAdded • ListDeleting, ListDeleted • SPListEventProperties object public class MyAuditTrail : SPListEventReceiver { public override void FieldDeleting(SPListEventProperties properties) { string usrName = properties.UserLoginName; string listTitle = properties.ListTitle; ... base.FieldDeleting(properties); } }

  10. Creating Web Event Receivers • SPWebEventReceiver class • Overriding events • SiteDeleting, SiteDeleted • WebDeleting, WebDeleted • WebMoving, WebMoved • WebAdding, WebProvisioned • SPWebEventProperties object public class MyTopologyManager : SPWebEventReceiver { public override void WebAdding(SPWebEventProperties properties) { intcurrentCount = properties.Web.ParentWeb.Webs.Count; ... base.WebAdding(properties); } }

  11. Creating Feature Receivers • Visual Studio Feature Designer • SPFeatureReceiver class • Overriding events • FeatureActivated • FeatureDeactivating • FeatureInstalled • FeatureUninstalling • FeatureUpgrading • SPFeatureReceiverProperties public class MyTopologyManager : SPWebEventReceiver { public override void FeatureActivated (SPFeatureReceiverProperties properties) { setProliferationFlag(true); } public override void FeatureDeactivating (SPFeatureReceiverProperties properties) { setProliferationFlag(false); } void setProliferationFlag(bool status) { if (status) { ... } else { ... } } }

  12. Canceling Events • Before Events • Controlling error behavior • CancelWithError status • CancelNoError status • CancelWithRedirectUrl status public class SchemaProtector : SPListEventReceiver { public override void FieldDeleting(SPListEventProperties properties) { if(properties.Field.Title == "MyRequiredDataField") { properties.ErrorMessage = "You must NOT delete this field."; properties.Status = SPEventReceiverStatus.CancelWithError; properties.Cancel = true; return; } base.FieldDeleting(properties); } }

  13. Lesson 2: Working with Web.Config Programmatically • Web Applications and Web.Config • Adding Web.Config Entries • Retrieving Web.Config Entries

  14. Web Applications and Web.Config • Each Web application has a Web.config file • IIS Web site • Web.config changes affect all site collections in the Web application • Farm considerations • Synchronizing Web servers

  15. Adding Web.Config Entries • SPWebConfigModificationclass void setProliferationFlag() { SPWebApplicationwebApp = SPWebApplication.Lookup(new Uri("http://SharePoint")); SPWebConfigModificationmySetting = null; mySetting = new SPWebConfigModification(); mySetting.Path = "configuration/appSettings"; mySetting.Name = "add [@key='preventProliferation'] [@value='1']"; mySetting.Sequence = 0; mySetting.Owner = "Lab05Owner"; mySetting.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; mySetting.Value = "<add key='preventProliferation' value='1' />"; webApp.WebConfigModifications.Add(mySetting); webApp.Update(); webApp.Farm.Services.GetValue<SPWebService>() .ApplyWebConfigModifications(); }

  16. Retrieving Web.Config Entries • ConfigurationManager class • AppSettings • ConnectionStrings • GetSection public class ControlSubsites : SPWebEventReceiver { public override void WebAdding(SPWebEventProperties properties) { string preventProliferation = ConfigurationManager.AppSettings["preventProliferation"].ToString(); if (preventProliferation == "1") { properties.ErrorMessage = "Sub webs are not allowed"; properties.Status = SPEventReceiverStatus.CancelWithError; properties.Cancel = true; return; } base.WebAdding(properties); } }

  17. Lab: Creating Event Receivers and Web.Config Modifications • Exercise 1: Creating List Event Receivers • Exercise 2: Creating Feature Receivers to Modify Web.Config • Exercise 3: Creating Web Event Receivers Logon information Estimated time: 45minutes

  18. Lab Review • What list event receivers did you create, and what did they do? • What feature receivers did you create, and what did they do?

  19. Module Review • In this module, you have learned to: • Create event receivers • Work with Web.config programmatically

More Related