In my project we have a SPLookupField developed through XML as,
<Field ID="{67146805-10A0-473F-971C-0B42BD36C2C9}" Name="My Reports" StaticName="Reports" Group="Custom Columns" Type="LookupMulti" DisplayName="My Reports" Description="List of associated reports" Mult="TRUE"
List="Lists/MyList" DisplaceOnUpgrade="TRUE" Sortable="TRUE" ShowField="Title" Required="True" />
This field is mapped to Title field of a Document Library named Source List.
Notice Name of the field has space between two words as "My Reports". Ya even I wondered but this field is deployed in SharePoint portal and working fine. I can edit the list item in List Library and change the lookup value. I can retrieve field's value through code but what I am not able to do is update the lookup value of this field.
I have a list named Master Reports in which this field is being used. When I change the value in LookupField (Title of the document) in Source List it doesn't reflect the updated value in Master Reports list in which this lookup field is being used. It doesn't show the updated value in Master Reports but when I click on the value it shows me the updated value. In List UI it doesn't refresh and show the updated value. I have tried following codes to change it,
SPFieldLookupValue sflv = new SPFieldLookupValue(50, "Test Value_changed");
SPFieldLookupValueCollection newReps = new SPFieldLookupValueCollection();
newReps.Add(sflv);
SPFieldLookup field = masterReport.Fields.GetFieldByInternalName("My Reports") as SPFieldLookup;
masterReportItem[field.Id] = newReps;
masterReportItem.Web.AllowUnsafeUpdates = true;
masterReportItem.Update();
I even tried following,
masterReport[field.Id] = newReps[0].LookupId;
and,
masterReport["My Reports"] = string.Format(CultureInfo.InvariantCulture, "{0};#{1}", newReps[0].LookupId.ToString(), newReps[0].LookupValue);
But nothing updates the value in Master Reports list library.
Funny thing is if I update the field through Powershell it works fine.
$mi["My Reports"] = "1;#Test_New1"
$mi.Web.AllowUnsafeUpdates = $true
$mi.Update()
$mi["My Reports"]
LookupId LookupValue
-------- -----------
1 Test_New1
Please let me know if I am missing something. Thanks in advance.