/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnBook_Click(object sender, EventArgs e) { if (ItemReferences.SiteRoot != null) { // Equivalent to Sitecore.Configuration.Factory.GetDatabase("master"); Database master = Sitecore.Data.Database.GetDatabase(dbMaster); Item bookingsRoot = master.GetItem(ItemReferences.BookingsRoot.ID); if (bookingsRoot != null) { // Rather than using the SecurityDisabler(), set up a user whose job is to create bookings, and use the UserSwitcher() to // run the code with their security privileges. using (new SecurityDisabler()) { string name = Regex.Replace(txtFirstName.Text + txtSurname.Text, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled).ToLower(); Guid dateGuid; if (Guid.TryParse(ddlHolidayDate.SelectedValue, out dateGuid)) { // create new booking Booking booking = new Booking(); booking.BookingItemName = Encoder.XmlEncode(name + DateUtil.IsoNow); booking.FirstName = Encoder.HtmlEncode(txtFirstName.Text); booking.Surname = Encoder.HtmlEncode(txtSurname.Text); booking.HolidayDate = dateGuid; booking.BookingsRoot = master.GetItem(ItemReferences.BookingsRoot.ID); // Execute booking pipeline HolidayBookingPipelineArgs pipelineArgs = new HolidayBookingPipelineArgs(Sitecore.Context.Item, booking); CorePipeline.Run("bookHoliday", pipelineArgs); if (!pipelineArgs.Valid && !string.IsNullOrEmpty(pipelineArgs.Message)) { // Execute code here to deal with failed validation } } } // get URL for thank-you page - specified in Sitecore - and redirect ReferenceField redirect = Sitecore.Context.Item.Fields[fnThankYouPage]; if (redirect.TargetItem != null) { Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(redirect.TargetItem), false); } } } }
public void Process(HolidayBookingPipelineArgs args) { if (args.Booking == null) { args.Valid = false; args.Message = "No booking item has been created"; } var booking = args.Booking; // create an item under the bookings root Item bookingItem = booking.BookingsRoot.Add(booking.BookingItemName, TemplateReferences.Booking); // populate the item with values from the transient booking item bookingItem.Editing.BeginEdit(); bookingItem.Fields[fnFirstName].Value = booking.FirstName; bookingItem.Fields[fnSurname].Value = booking.Surname; bookingItem.Fields[fnBookedDate].Value = booking.HolidayDate.ToString(); bookingItem.Editing.EndEdit(); }