Помимо запечатывания, поле может быть доступно только для чтения, скрыто и т. Д. Все эти факторы могут помешать удалению поля. Более подходящий метод удаления поля списка следующий:
public static bool RemoveField(SPField spField)
{
if (spField == null)
{
WriteErrorToLog("spField is null! Please, provide a valid one");
return false;
}
bool res = false;
try
{
// check if it's a ReadOnly field.
// if so, reset it
if (spField.ReadOnlyField)
{
spField.ReadOnlyField = false;
spField.Update();
}
// check if it's a Hidden field.
// if so, reset it
if (spField.Hidden)
{
spField.Hidden = false;
spField.Update();
}
// check if the AllowDeletion property is set to false.
// if so, reset it to true
if (spField.AllowDeletion == null || !spField.AllowDeletion.Value)
{
spField.AllowDeletion = true;
spField.Update();
}
// If the AllowDeletion property is set,
// the Sealed property seems not to be examined at all.
// So the following piece of code is commented.
/*if(spField.Sealed)
{
spField.Sealed = false;
spField.Update();
}*/
// If the AllowDeletion property is set,
// the FromBaseType property seems not to be examined at all.
// So the following piece of code is commented.
/*if(spField.FromBaseType)
{
spField.FromBaseType = false;
spField.Update();
}*/
// finally, remove the field
spField.Delete();
spField.ParentList.Update();
res = true;
}
catch (Exception ex)
{
WriteErrorToLog(ex.Message);
}
return res;
}
public static bool RemoveField(SPList spList, string displayNameOrInternalNameOrStaticName)
{
SPField spField = GetFieldByName(spList, displayNameOrInternalNameOrStaticName);
if(spField == null)
{
WriteErrorToLog(string.Format("Couldn't find field {0}!", displayNameOrInternalNameOrStaticName));
return false;
}
return RemoveField(spField);
}
public static void WriteErrorToLog(string errorMsg)
{
// write error into log
}
Прочтите статью , чтобы узнать больше.