我正在尝试向 2 个现有内容类型添加 2 个新字段,但我遇到了这个异常,不知道如何修复它
在该行中的第一个内容类型更新后引发异常:agendaPoints.AddFieldRefFromContentType(currentWeb, fldRecurrent);
private void AddRecurrentAndCopyAttachmentsFieldsToContentType(SPWeb currentWeb)
{
try
{
currentWeb.AllowUnsafeUpdates = true;
SPContentType agendaPointsProposedCT = currentWeb.ContentTypes[Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME];
SPContentType agendaPoints = currentWeb.ContentTypes[Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINT_NAME];
string recurrentFieldName = currentWeb.Fields.Add(Meetings.Common.Constants.FIELDS_AGENDAPOINTSRECURRENT_NAME, SPFieldType.Boolean, false);
SPField recurrentField = currentWeb.Fields.GetFieldByInternalName(recurrentFieldName);
string copyAttachmentsFieldName = currentWeb.Fields.Add(Meetings.Common.Constants.FIELDS_AGENDAPOINTSCOPYATTACHMENTS_NAME, SPFieldType.Boolean, false);
SPField copyAttachmentsField = currentWeb.Fields.GetFieldByInternalName(recurrentFieldName);
SPField fldRecurrent = currentWeb.Fields[Meetings.Common.Constants.FIELDS_AGENDAPOINTSRECURRENT_NAME];
SPField fldCopyAttachments = currentWeb.Fields[Meetings.Common.Constants.FIELDS_AGENDAPOINTSCOPYATTACHMENTS_NAME];
agendaPointsProposedCT.AddFieldRefFromContentType(currentWeb, fldRecurrent);
agendaPointsProposedCT.AddFieldRefFromContentType(currentWeb, fldCopyAttachments);
MoveFieldInColumnOrder(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, fldRecurrent.InternalName, 1);
MoveFieldInColumnOrder(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, fldCopyAttachments.InternalName, 2);
agendaPointsProposedCT.Update();
currentWeb.AllowUnsafeUpdates = true;
agendaPoints.AddFieldRefFromContentType(currentWeb, fldRecurrent);
agendaPoints.AddFieldRefFromContentType(currentWeb, fldCopyAttachments);
MoveFieldInColumnOrder(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINT_NAME, fldRecurrent.InternalName, 1);
MoveFieldInColumnOrder(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINT_NAME, fldCopyAttachments.InternalName, 2);
agendaPoints.Update();
}
catch (Exception ex)
{
throw;
}
finally
{
currentWeb.AllowUnsafeUpdates = false;
}
}
和扩展方法
public static void AddFieldRefFromContentType(this SPContentType contentType, SPWeb web,SPField field)
{
SPFieldLink fieldLink = new SPFieldLink(web.AvailableFields.GetField(field.InternalName));
//Check if the Field reference exists
if (!contentType.Fields.ContainsField(field.Title))
{
contentType.FieldLinks.Add(fieldLink);
contentType.Update(true);
}
else
{
//Do Nothing
}
}