2012-03-06 28 views
1

我需要一些幫助來了解如何恢復我的某個SharePoint列表的查找列的值。如何重置Sharepoint列表的查找值?

場景: 我有2個共享點列表。 LIST1 LIST2

LIST1有一個A類型的擴展查找字段,它引用了LIST2的A列。

最近,我在LIST2中添加了另一個字段。然後我執行,停用/激活|卸載/安裝LIST2。

NOW <問題是A列的參考查找 - LIST1 TO列A - LIST2丟失。

得到信息: 編輯列A-LIST1時之前,還有下寫信息 列 - 列表2

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

+0

你可以發佈截圖嗎? – 2012-03-06 07:56:22

回答

1

LookupList屬性包含GUID的LIST2的原始實例。如果刪除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財產。

1

這是另一種可能會重置目標網站和列表的方法。代碼放入擴展方法中。我從一些其他的網絡帖子的基本思想:

擴展方法,修改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); 
    } 

} 
相關問題