protected void btnSave_Click(object sender, EventArgs e) { try { IDbFieldEditor editor = holderExtraSettings.FindControl(EDITOR_CONTROL_ID) as IDbFieldEditor; if (editor == null) { throw new Exception("Cannot find Field Editor on page!"); } DbField fld = editor.GetField(txtColName.Text); ListMetadataField metaField = this.Field; metaField.FieldName = fld.DisplayName; metaField.InternalName = ddlTableFieldName.SelectedValue; metaField.Description = txtDescription.Text; metaField.Required = fld.Required; metaField.DefaultValue = fld.DefaultValue; metaField.SetControlProperties(fld); if (fld.Type == SPFieldType.Lookup) { var loFld = fld as DbFieldLookup; if (loFld != null) { metaField.DataSourceType = loFld.ListSource; metaField.DataSource = loFld.ListId; metaField.DataSourceKey = loFld.LookupKey; metaField.DataSourceField = loFld.LookupField; } } else if (fld.Type == SPFieldType.User) { var usFld = fld as DbFieldUser; if (usFld != null) { metaField.DataSourceField = usFld.LookupField; } } else { metaField.DataSourceType = (int)LookupSourceType.None; metaField.DataSource = null; metaField.DataSourceKey = null; metaField.DataSourceField = null; } new RosterConfigService().SaveField(metaField); // close form Utils.GoBackOnSuccess(this, this.Context); } catch (Exception ex) { holderExtraSettings.Controls.Add(new Label() { Text = ex.Message, ForeColor = System.Drawing.Color.Red }); } }
protected void btnSave_Click(object sender, EventArgs e) { try { var editor = holderExtraSettings.FindControl(EDITOR_CONTROL_ID) as IDbFieldEditor; if (editor == null) { throw new Exception("Cannot find Field Editor on page!"); } var fld = editor.GetField(txtColName.Text); if (List.ListMetadataFields.Any(f => f.FieldName.ToNormalize().ToLower().Equals(fld.DisplayName.ToNormalize().ToLower()))) { throw new Exception("Column with a name " + txtColName.Text + " already exists in a list!"); } if (chListContentTypes.SelectedIndex < 0) { throw new Exception("Please select minimum one Content Type!"); } var internalName = cbCreateNewColunm.Checked || string.IsNullOrWhiteSpace(ddlTableFieldName.SelectedValue) ? string.Format("{0}{1}", fld.DisplayName.ToNormalize(), Guid.NewGuid().ToString("N")) : ddlTableFieldName.SelectedValue; var metaField = new ListMetadataField { Id = Guid.NewGuid(), InternalName = internalName, FieldName = fld.DisplayName, Description = txtDescription.Text, ListMetadataId = List.Id, Required = fld.Required, FieldType = fld.SqlType, DefaultValue = fld.DefaultValue }; metaField.SetControlProperties(fld); if (fld.Type == SPFieldType.Lookup) { var loFld = fld as DbFieldLookup; if (loFld != null) { metaField.DataSourceType = loFld.ListSource; metaField.DataSource = loFld.ListId; metaField.DataSourceKey = loFld.LookupKey; metaField.DataSourceField = loFld.LookupField; } } else if (fld.Type == SPFieldType.User) { var usFld = fld as DbFieldUser; if (usFld != null) { metaField.DataSourceType = usFld.ListSource; metaField.DataSource = usFld.ListId; metaField.DataSourceKey = usFld.LookupKey; metaField.DataSourceField = usFld.LookupField; } } configProvider.SaveField(metaField); // update Content types var listContentTypes = this.List.ListMetadataContentTypes; foreach (ListItem ctItem in chListContentTypes.Items) { if (!ctItem.Selected) { continue; } var ct = listContentTypes.FirstOrDefault(cot => cot.Id == Int32.Parse(ctItem.Value)); if (ct != null) { ct.ListMetadataFieldContentTypes.Add(new ListMetadataFieldContentType { Id = Guid.NewGuid(), ContentTypeId = ct.Id, ListMetadataFieldId = metaField.Id }); } // update ContentType in DB configProvider.SaveContentType(ct); } // close form Utils.GoBackOnSuccess(this, this.Context); } catch (Exception ex) { holderExtraSettings.Controls.Add(new Label { Text = ex.Message, ForeColor = System.Drawing.Color.Red }); } }