【发布时间】:2012-08-09 04:17:58
【问题描述】:
我正在尝试向 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
}
}
【问题讨论】:
-
您是否尝试将获取内容类型的行移动到允许不安全更新的上方/下方的行。内容类型之间是否还有任何类型的继承设置?
-
确实问题不在代码中,问题在于实际上议程点是从议程点提议的内容类型继承的,所以我只需要将字段添加一次到父内容类型就可以了.感谢您帮助我找出问题:),发布答案我会将其标记为已回答:)
标签: c# sharepoint sharepoint-2010