How to reset the LookUp value of a Sharepoint List

sharepoint

I need some help on how to restore the value of the lookup column of one of my sharepoint list.

Scenario:
I have 2 sharepoint lists.
LIST1
LIST2

LIST1 has a column A with type Extended Lookup Field, this references to column A of LIST2.

Recently, I added another field in LIST2. Then I performed, deactivate/activate | uninstall/install of LIST2.

NOW< the problem is the reference lookup of column A – LIST1 TO column A – LIST2 was lost.
Before when editing column A-LIST1, there is information written under:

Get information from:
column a – LIST2

Now, it's just blank…

Best Answer

The LookupList property contains the GUID of the original instance of LIST2. If LIST2 was deleted and a new instance was created, the new LIST2 will have a different GUID and your lookup field on LIST1 will not work.

And, unfortunately, LookupList cannot be changed directly:

SPException: The property has already been set. You cannot change the lookup list after the LookupList property is set.

However, you can try the following:

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

Using reflection, you can try to call the internal SetFieldAttributeValue method and change the "List" attribute, which is what is used by the LookupList property.

Related Topic