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();

SPFieldLookup field = masterReport.Fields.GetFieldByInternalName("My Reports") as SPFieldLookup;
masterReportItem[field.Id] = newReps;
masterReportItem.Web.AllowUnsafeUpdates = true;

I even tried following,

masterReport[field.Id] = newReps[0].LookupId;


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["My Reports"]

   LookupId LookupValue
   -------- -----------
          1 Test_New1

Please let me know if I am missing something. Thanks in advance.

  • Hi Guys, Let me make it more clear actually, I am changing the Lookup value in source list (to which this lookup field is mapped to, Title field of a document library) and this field is added in a target list. So when I change the title of the document it should reflect the change in target list. But in target list it doesn't show the updated value. As Title is a link field so when I click on the title link of that document in target list it shows me view properties page of that document from Source Document Library. In that view prop page it shows me updated value. So that's the issue. – Syed Abbas Nov 11 '16 at 16:48

The right way to set the value is:

listItem["field"] = new SPFieldLookupValue(1, "text"); //the ID must exist

If you dont have one of the parameters, you will have to perform a "query" to get it.

SPFieldLookupValue fieldLookupValue = new SPFieldLookupValue(item["FieldName"].ToString());
int lookupID = fieldLookupValue.LookupId;
string lookupValue = fieldLookupValue.LookupValue;

If you need multiple values the answer is

SPFieldLookupValueCollection fieldValues = new

fieldValues.Add (new SPFieldLookupValue(1, "SomeValue1"));  // add other field values or in loop

item["lookupmultiField"] = fieldValues;

try that, greetings

| improve this answer | |
  • This is the answer. Lookup Fields are tied by the originating lookup field ID. – lazoDev Nov 10 '16 at 19:30
  • Hi Marco, Thanks for the response. I checked and can tell there is no constructor accepting 1 parameter ID as integer. One argument constructor accepts string value which is lookup value and not lookup id. – Syed Abbas Nov 11 '16 at 14:38
  • Hi Sayed, you are right. I edit my answer. – Marco Nov 11 '16 at 15:03
  • Still it didn't work. Please check my comment to the original post. – Syed Abbas Nov 11 '16 at 17:00
  • I'm quite sure that this is the answer. Check if the values exist in the Parent List. In your post you mention a SPFieldLookupValueCollection is this field a lookupField with multiple values? Provide us the error – Marco Nov 11 '16 at 17:25

Hi the lookup field accepts the item id as parameter,try with below code

listItem["field"] =GetLookup("Text to update");

private SPFieldLookupValue GetLookup(string Name)
        SPFieldLookupValue spLookupName = null;
            SPList lst = SPContext.Current.Web.Lists.TryGetList("List");
            if (lst != null)
                SPQuery query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + Name + "</Value></Eq></Where>";

                SPListItemCollection listColl = lst.GetItems(query);
                if (listColl.Count > 0)
                    foreach (SPListItem item in listColl)
                        if (item["Title"] != null)
                           spLookupName = new SPFieldLookupValue(Convert.ToInt32(item["ID"]), Convert.ToString(item["Title"]));                                
        catch (Exception ex)
            WriteToLogFile("GetLookup() : " + ex.Message + ex.StackTrace);
        return spLookupName ;
| improve this answer | |
  • Hi Sunil, I have tried this as well but it doesn't work. – Syed Abbas Nov 11 '16 at 16:44
  • Where List is the Parent list used for lookup – Sunil Sahu Nov 14 '16 at 7:24
  • Hi Sunil, Please see my comment to Marco on Nov 18 at 10:25 – Syed Abbas Nov 18 '16 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.