protected void btnSave_Click(object sender, EventArgs e) { if (lopAddress.Location == null || string.IsNullOrWhiteSpace(lopAddress.Location.Street1) || string.IsNullOrWhiteSpace(lopAddress.Location.PostalCode)) { nbValidation.Visible = true; ScriptManager.RegisterStartupScript(Page, this.GetType(), "ScrollPage", "setTimeout(function(){window.scroll(0,0);},200)", true); return; } else { nbValidation.Visible = false; } if (_group == null) { ShowMessage("There was an issue with the viewstate. Please reload and try again. If the problem perssits contact an administrator.", "Error", "panel panel-danger"); return; } GroupService groupService = new GroupService(_rockContext); //Add basic information Group group; if (_group.Id == 0) { group = new Group() { GroupTypeId = _groupType.Id }; var destinationGroup = groupService.Get(GetAttributeValue("DestinationGroup").AsGuid()); if (destinationGroup != null) { group.ParentGroupId = destinationGroup.Id; } var parentMapping = GetAttributeValue("CampusGroupMap").ToKeyValuePairList(); foreach (var pair in parentMapping) { if (pair.Key.AsInteger() == cpCampus.SelectedValueAsId()) { group.ParentGroupId = (( string )pair.Value).AsInteger(); } } var zip = "No Zip"; if (lopAddress.Location != null && !string.IsNullOrWhiteSpace(lopAddress.Location.PostalCode)) { zip = lopAddress.Location.PostalCode; } group.Name = string.Format("{0} - {1}", tbName.Text, zip); groupService.Add(group); group.CreatedByPersonAliasId = _person.PrimaryAliasId; group.IsActive = true; } else { group = groupService.Get(_group.Id); group.IsActive = true; } group.CampusId = cpCampus.SelectedValueAsId(); group.Description = tbDescription.Text; //Set location if (lopAddress.Location != null && lopAddress.Location.Id != 0) { if (group.GroupLocations != null && !group.GroupLocations.Select(gl => gl.LocationId).Contains(lopAddress.Location.Id)) { // Disassociate the old address(es) GroupLocationService groupLocationService = new GroupLocationService(_rockContext); var meetingLocationDv = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_MEETING_LOCATION); var homeLocationDv = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME); groupLocationService.DeleteRange(group.GroupLocations.Where(gl => gl.GroupLocationTypeValueId == meetingLocationDv.Id || gl.GroupLocationTypeValueId == homeLocationDv.Id || gl.GroupLocationTypeValueId == null)); group.GroupLocations.Add(new GroupLocation() { LocationId = lopAddress.Location.Id, GroupLocationTypeValueId = meetingLocationDv.Id }); } } //Set Schedule if (_groupType.AllowedScheduleTypes != ScheduleType.None) { switch (rblSchedule.SelectedValueAsEnum <ScheduleType>()) { case ScheduleType.None: group.ScheduleId = null; break; case ScheduleType.Weekly: var weeklySchedule = new Schedule() { WeeklyDayOfWeek = dowWeekly.SelectedDayOfWeek, WeeklyTimeOfDay = timeWeekly.SelectedTime }; group.Schedule = weeklySchedule; break; case ScheduleType.Custom: var customSchedule = new Schedule() { iCalendarContent = sbSchedule.iCalendarContent }; group.Schedule = customSchedule; break; case ScheduleType.Named: if (spSchedule.SelectedValue.AsInteger() != 0) { group.ScheduleId = spSchedule.SelectedValue.AsInteger(); } break; default: break; } } if (group.Members != null && group.Members.Any()) { foreach (var member in group.Members.Where(gm => gm.PersonId != _person.Id)) { member.GroupMemberStatus = GroupMemberStatus.Inactive; } foreach (int groupMemberId in gMembers.SelectedKeys) { var groupMember = group.Members.Where(m => m.Id == groupMemberId).FirstOrDefault(); if (groupMember != null) { groupMember.GroupMemberStatus = GroupMemberStatus.Active; } } } else { var groupRoleId = new GroupTypeRoleService(_rockContext).Get(GetAttributeValue("GroupRole").AsGuid()).Id; group.Members.Add(new GroupMember() { PersonId = _person.Id, GroupRoleId = groupRoleId }); } //Save attributes _rockContext.SaveChanges(); group.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAttributes, group); var attributeGuid = GetAttributeValue("MultiSelectAttribute"); var multiselectAttribute = new AttributeService(_rockContext).Get(attributeGuid.AsGuid()); if (multiselectAttribute != null) { var attributeValue = group.GetAttributeValue(multiselectAttribute.Key) .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .ToList(); var newAttributeText = GetAttributeValue("AttributeText"); if (!attributeValue.Contains(newAttributeText)) { attributeValue.Add(newAttributeText); } group.SetAttributeValue(multiselectAttribute.Key, string.Join(",", attributeValue)); } group.SaveAttributeValues(); //Update cache var availableGroupIds = (List <int>)GetCacheItem(GetAttributeValue("DestinationGroup")); if (availableGroupIds != null) { availableGroupIds.Add(group.Id); AddCacheItem(GetAttributeValue("DestinationGroup"), availableGroupIds); } var workflowTypeService = new WorkflowTypeService(_rockContext); WorkflowTypeCache workflowType = null; Guid?workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull(); if (workflowTypeGuid.HasValue) { workflowType = WorkflowTypeCache.Get(workflowTypeGuid.Value); } GroupMember currentGroupMember = group.Members.Where(gm => gm.PersonId == _person.Id).FirstOrDefault(); if (currentGroupMember != null && workflowType != null && (workflowType.IsActive ?? true)) { try { List <string> workflowErrors; var workflow = Workflow.Activate(workflowType, _person.FullName); if (workflow.AttributeValues.ContainsKey("Group")) { if (group != null) { workflow.AttributeValues["Group"].Value = group.Guid.ToString(); } } if (workflow.AttributeValues.ContainsKey("Person")) { if (_person != null) { workflow.AttributeValues["Person"].Value = _person.PrimaryAlias.Guid.ToString(); } } new WorkflowService(_rockContext).Process(workflow, currentGroupMember, out workflowErrors); } catch (Exception ex) { ExceptionLogService.LogException(ex, this.Context); } } ShowMessage(GetAttributeValue("SuccessText"), "Thank you!", "panel panel-success"); }