private void UpdateFieldVisibility(SPField field, BaseFieldInfo fieldInfo) { field.ShowInListSettings = !fieldInfo.IsHiddenInListSettings; field.ShowInDisplayForm = !fieldInfo.IsHiddenInDisplayForm; field.ShowInEditForm = !fieldInfo.IsHiddenInEditForm; field.ShowInNewForm = !fieldInfo.IsHiddenInNewForm; // Apply Hidden here again (even through it's already set through the schema XML), // because otherwise updates to Hidden will not work. if (!field.CanToggleHidden) { bool before = field.Hidden; // Use reflection to get around the CanToggleHidden constraint. Keep in mind that // there may be some unintended consequenced from hiding/showing and previously // shown/hidden field (hence the logged warning). Type type = field.GetType(); MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(field, new object[] { "CanToggleHidden", true }); field.Hidden = fieldInfo.IsHidden; mi.Invoke(field, new object[] { "CanToggleHidden", false }); this.log.Warn( string.Format( CultureInfo.InvariantCulture, "FieldHelper.EnsureField - Forced field (id={0}, name={1}) from Hidden={2} to Hidden={3} even though it should've been impossible because CanToggleHidden=false.", field.Id, field.InternalName, before, fieldInfo.IsHidden)); } else { // No need to use reflection before being able to set the Hidden property field.Hidden = fieldInfo.IsHidden; } }
/// <summary> /// Runs the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; SPBinaryParameterValidator.Validate("fielddisplayname", Params["fielddisplayname"].Value, "fieldinternalname", Params["fieldinternalname"].Value); string url = Params["url"].Value; string fieldTitle = Params["fielddisplayname"].Value; string fieldName = Params["fieldinternalname"].Value; bool useTitle = Params["fielddisplayname"].UserTypedIn; bool useName = Params["fieldinternalname"].UserTypedIn; bool force = Params["force"].UserTypedIn; SPField field = Utilities.GetField(url, fieldName, fieldTitle, useName, useTitle); if (field.ReadOnlyField && force) { field.ReadOnlyField = false; field.Update(); } if (!field.CanBeDeleted) { if (field.FromBaseType) { throw new Exception( "The field is derived from a base type and cannot be deleted. You must delete the field from the base type."); } else if (field.Sealed) { if (force) { field.Sealed = false; field.Update(); } else { throw new Exception("This field is sealed and cannot be deleted - specify \"-force\" to ignore this setting and attempt deletion regardless."); } } else if (field.AllowDeletion.HasValue && !field.AllowDeletion.Value && !force) { throw new Exception( "Field is marked as not allowing deletion - specify \"-force\" to ignore this setting and attempt deletion regardless."); } else if (field.AllowDeletion.HasValue && !field.AllowDeletion.Value && force) { field.AllowDeletion = true; field.Update(); } else { throw new Exception("Field cannot be deleted."); } } if (field.Hidden) { if (force) { if (field.CanToggleHidden) { field.Hidden = false; field.Update(); } else { MethodInfo setFieldBoolValue = field.GetType().GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new Type[] { typeof(string), typeof(bool) }, null); //field.SetFieldBoolValue("Hidden", false); setFieldBoolValue.Invoke(field, new object[] { "Hidden", false }); //field.SetFieldBoolValue("CanToggleHidden", true); setFieldBoolValue.Invoke(field, new object[] { "CanToggleHidden", true }); field.Update(); } } else { throw new Exception( "You cannot delete hidden fields - specify \"-force\" to ignore this restriction and attempt deletion regardless."); } } field.Delete(); return((int)ErrorCodes.NoError); }