我正在尝试向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
}
}
答案 0 :(得分:2)
您是否尝试将获取内容类型的行移至Allow Unsafe更新上方/下方的行。内容类型之间是否还有任何类型的继承设置?