190 likes | 206 Views
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
E N D
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 • 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
Types of Event Targets • List items • List e-mails • List workflows • Lists • Webs • Features
Types of Events • Before events (synchronous) • After events (asynchronous)
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); } }
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); } }
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); } }
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); } }
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); } }
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 { ... } } }
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); } }
Lesson 2: Working with Web.Config Programmatically • Web Applications and Web.Config • Adding Web.Config Entries • Retrieving Web.Config Entries
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
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(); }
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); } }
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
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?
Module Review • In this module, you have learned to: • Create event receivers • Work with Web.config programmatically