【问题标题】:How can I reset the LookUp value of a Sharepoint List?如何重置 Sharepoint 列表的 LookUp 值?
【发布时间】:2015-03-12 19:53:37
【问题描述】:

我需要一些有关如何恢复我的共享点列表之一的查找列的值的帮助。

场景: 我有 2 个共享点列表。 列表1 清单2

LIST1 有一个类型为扩展查找字段的 A 列,这引用了 LIST2 的 A 列。

最近,我在 LIST2 中添加了另一个字段。然后我执行,停用/激活 |卸载/安装 LIST2。

NOW

从以下位置获取信息: a 列 - LIST2

现在,它只是空白......

【问题讨论】:

    标签: sharepoint


    【解决方案1】:

    LookupList 属性包含 LIST2 的原始实例的 GUID。如果删除了 LIST2 并创建了新实例,则新的 LIST2 将具有不同的 GUID,并且您在 LIST1 上的查找字段将不起作用。

    很遗憾,LookupList 不能直接更改:

    SPException:该属性已设置。设置 LookupList 属性后,您无法更改查找列表。

    但是,您可以尝试以下方法:

    Type type = typeof(SPFieldLookup);
    object obj = type.InvokeMember("SetFieldAttributeValue", 
        BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, 
        null, 
        myLookupField, 
        new object[] { "List", guidOfNewList.ToString() });
    myLookupField.Update();
    

    使用反射,可以尝试调用内部的SetFieldAttributeValue方法,改变LookupList属性使用的“List”属性。

    【讨论】:

      【解决方案2】:

      这是另一种重置目标网站和列表的方法。代码被放置在扩展方法中。我从其他一些互联网帖子中获得了基本想法:

      扩展方法,用字符串操作修改XML: http://blogs.edwardwilde.com/2010/02/08/cannot-change-the-lookup-list-of-the-lookup-field/

      使用 XmlDocument 的代码片段: http://social.msdn.microsoft.com/Forums/sharepoint/en-US/f5c421a2-ca88-414e-9110-2b8ecb716e54/reattach-a-sharepoint-list-lookup-column-to-the-source-list

      什么对我的项目有用:

      /// <summary>
      /// Extension methods for SPField objects.
      /// </summary>
      public static class SPFieldLookupExtensions
      {
      
          /// <summary>
          /// Updates a Lookup field's source list by directly manipulating the XML schema SharePoint uses for the field.
          /// </summary>
          /// <param name="lookupField">The field to be updated.</param>
          /// <param name="list">The list that should be used by the lookup field.</param>
          public static void UpdateLookupReferences(this SPFieldLookup lookupField, SPList list)
          {
              // whether or not the lookup field's list is in the same site as the target list
              bool differentSite = lookupField.LookupWebId != list.ParentWeb.ID;
              // whether or not the lookup field's target list is correctly set
              bool differentList = lookupField.LookupList != list.ID.ToString();
      
              if (!differentSite && !differentList)
              {
                  // return if field's properties are already correct.
                  return;
              }
      
              if (string.IsNullOrEmpty(lookupField.LookupList) && (!differentSite || (differentSite && string.IsNullOrEmpty(lookupField.LookupWebId.ToStringNullSafe()))))
              {
                  // if field has not been bound to anything, bind it now
                  if (differentSite)
                  {
                      lookupField.LookupWebId = list.ParentWeb.ID;
                  }
                  lookupField.LookupList = list.ID.ToString();
              }
              else
              {
                  // field is incorrectly bound, fix it.
                  XmlDocument fieldSchema = new XmlDocument();
                  fieldSchema.LoadXml(lookupField.SchemaXml);
                  if (differentSite)
                  {
                      XmlAttribute webAttr = fieldSchema.DocumentElement.Attributes["WebId"];
                      if (webAttr == null)
                      {
                          webAttr = fieldSchema.CreateAttribute("WebId");
                          fieldSchema.DocumentElement.SetAttributeNode(webAttr);
                      }
                      webAttr.Value = list.ParentWeb.ID.ToString();
                  }
      
                  if (differentList)
                  {
                      XmlAttribute listAttr = fieldSchema.DocumentElement.Attributes["List"];
                      if (listAttr == null)
                      {
                          listAttr = fieldSchema.CreateAttribute("List");
                          fieldSchema.DocumentElement.SetAttributeNode(listAttr);
                      }
                      listAttr.Value = list.ID.ToString();
                  }
                  lookupField.SchemaXml = fieldSchema.InnerXml;
              }
      
              lookupField.Update(true);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 2013-01-24
        • 2011-07-07
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2018-07-19
        • 2011-08-18
        • 1970-01-01
        相关资源
        最近更新 更多