【问题标题】:Adding 2 fields to 2 content types give: the object has been updated by another user since it was last fetched将 2 个字段添加到 2 个内容类型给出:对象自上次获取以来已被另一个用户更新
【发布时间】: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


【解决方案1】:

您是否尝试过将获取内容类型的行移至“允许不安全更新”上方/下方的行。内容类型之间是否还有任何类型的继承设置?

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2015-04-19
    • 2019-08-10
    • 1970-01-01
    • 2021-09-28
    • 2021-12-10
    • 2016-09-11
    • 2011-07-28
    相关资源
    最近更新 更多