protected void ScheduleListView_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e == null || e.CommandArgument == null) { return; } var id = new Guid(e.CommandArgument.ToString()); switch (e.CommandName) { case "AddToSchedule": { // add this to the user's schedule var sessionSchedule = XrmContext.CreateQuery("adx_eventschedule").First(es => es.GetAttributeValue <Guid>("adx_eventscheduleid") == id); var registration = new Entity("adx_eventregistration"); registration.SetAttributeValue("adx_attendeeid", Attendee.ToEntityReference()); registration.SetAttributeValue("adx_eventid", sessionSchedule.GetAttributeValue <EntityReference>("adx_eventid")); registration.SetAttributeValue("adx_eventscheduleid", sessionSchedule.ToEntityReference()); registration.SetAttributeValue("adx_registrationdate", DateTime.UtcNow); XrmContext.AddObject(registration); XrmContext.SaveChanges(); } break; case "RemoveFromSchedule": { // remove this from the user's schedule var sessionSchedule = XrmContext.CreateQuery("adx_eventschedule").First(es => es.GetAttributeValue <Guid>("adx_eventscheduleid") == id); var eventSchedules = sessionSchedule.GetRelatedEntities(XrmContext, new Relationship("adx_eventschedule_eventregistration")).ToList(); var registration = eventSchedules.FirstOrDefault(er => er.GetAttributeValue <EntityReference>("adx_attendeeid") == Attendee.ToEntityReference()); XrmContext.DeleteObject(registration); XrmContext.SaveChanges(); } break; case "Feedback": { var feedbackPage = ServiceContext.GetPageBySiteMarkerName(Website, "Event Feedback"); if (feedbackPage != null) { var url = new UrlBuilder(ServiceContext.GetUrl(feedbackPage)); url.QueryString["id"] = id.ToString(); Response.Redirect(url.PathWithQueryString); } } break; } // redirect to current page (to avoid re-post issues) if (SiteMap.CurrentNode == null) { return; } Response.Redirect(SiteMap.CurrentNode.Url); }