【问题标题】:How do you remove/delete a column in a custom list that you added from existing site columns?如何删除/删除从现有网站栏添加的自定义列表中的栏?
【发布时间】:2010-12-06 08:45:41
【问题描述】:

我有一个自定义列表,并且通过单击列表的“设置”页面上的“从现有网站栏添加”链接添加了一个“页面图像”字段。我现在想删除该字段,但单击“设置”页面上的字段名称不会产生“删除”功能。

如何从 SharePoint 中的自定义列表中删除通过“从现有网站栏添加”菜单项添加的字段?

【问题讨论】:

    标签: sharepoint moss sharepoint-2007


    【解决方案1】:

    “页面图像”是一种特殊类型的 SharePoint 字段,定义为 密封。这意味着一旦添加它就无法从 UI 中删除。但是可以通过编程方式将其删除:

    SPList list = web.Lists["CustomTest"];
    SPField f = list.Fields["Page Image"];
    f.Sealed = false;
    f.Update();
    list.Fields["Page Image"].Delete();
    

    作为参考,该字段在C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingResources\PublishingColumns.xml中定义。

    【讨论】:

      【解决方案2】:

      除了被密封之外,一个字段可以是只读的、隐藏的等等。所有这些因素都可能阻止该字段被删除。更适合删除列表字段的方法如下:

      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
      }
      

      阅读完整的article on my blog,了解更多信息。

      【讨论】:

        【解决方案3】:
        1. 转到文档库设置页面中的“高级设置”。

        2. 在单选字段“允许管理内容类型”中,单击“是”并返回文档库设置

        3. 在名为“内容类型”的新部分下,单击“文档”

        4. 单击要删除的字段;您应该会看到一个“删除”按钮。

        【讨论】:

        • 在第 3 步之前,我一直按照您的指示进行操作 - 这是一个自定义列表,而不是文档库,因此我将“项目”视为“内容类型”下列出的唯一内容,而不是“文档”。尽管单击 Item 会产生预期的字段列表。但是,当我单击要删除的字段时,唯一的选项是“必需”、“可选”和“隐藏”——没有“删除”按钮。不幸的是,“隐藏”不足以满足我的目的。还有其他想法吗?
        • 我能想到的唯一其他选择是使用对象模型来获取内容类型并以编程方式关闭额外字段。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多