locked
Lookup field value is not getting set when using Client object model RRS feed

  • Question

  • I am very new to SharePoint. 

    I have a document library (contact). Have created a custom list (workstatus). Have added column to contact library which is lookup of workstatus.

    Below is my code using which i am setting the lookup column value. Unfortunately code runs fine (no exceptions) but when i check in SharePoint value is not set for the given column.

    FieldLookupValue lvworkstatus = new FieldLookupValue();
    lvworkstatus.LookupId = workstatus;
    item["crmworkstatus"] = lvworkstatus;
     item.Update();
    context.Load(uploadFile);
    context.ExecuteQuery();

    kindly let know what is wrong / missing.

    Thanks in advance


    Sunday, July 5, 2015 9:22 AM

Answers

  • Hi,

    According to your description, my understanding is that you want to set look up field value using Client Object Model.

    Here is a code snippet for your reference:

    public static void UpdateListitemLookup()
     {
                ClientContext clientContext = new ClientContext("http://sp2013sps/sites/test1");
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                FieldCollection fields = list.Fields;
                CamlQuery camlQueryForItem = new CamlQuery();
                camlQueryForItem.ViewXml = @"<View>
                                        <Query>
                                            <Where>
                                                <Eq>
                                                    <FieldRef Name='ID'/>
                                                    <Value Type='Counter'>6</Value>
                                                </Eq>
                                            </Where>
                                        </Query>
                                    </View>";
                ListItemCollection listItems = list.GetItems(camlQueryForItem);
                clientContext.Load(listItems, items => items.Include
                                                (item => item["wlookup"],
                                                 item => item["Editor"],
                                                 item => item["Title"]));
                clientContext.ExecuteQuery();
                ListItem itemToUpdate = listItems[0];
                FieldLookupValue lv = itemToUpdate["wlookup"] as FieldLookupValue;
                lv.LookupId = 16;
                itemToUpdate["wlookup"] = lv;
                itemToUpdate.Update();
                clientContext.Load(itemToUpdate);
                clientContext.ExecuteQuery();
            }

    In your code snippet, you need to load the item after item. Update()  method.

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, July 6, 2015 6:10 AM
  • Hi Shoab,

    LookUpField can only update by using LookUpID which you can find from LookUPList.

    Please try to update the column using LookUpID in stead of value.

    Try This : - https://rmanimaran.wordpress.com/2012/07/16/update-lookup-field-using-client-object-model/

    regards,

    Gaurav

    Saturday, July 11, 2015 6:22 AM

All replies

  • Hi,

    According to your description, my understanding is that you want to set look up field value using Client Object Model.

    Here is a code snippet for your reference:

    public static void UpdateListitemLookup()
     {
                ClientContext clientContext = new ClientContext("http://sp2013sps/sites/test1");
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                FieldCollection fields = list.Fields;
                CamlQuery camlQueryForItem = new CamlQuery();
                camlQueryForItem.ViewXml = @"<View>
                                        <Query>
                                            <Where>
                                                <Eq>
                                                    <FieldRef Name='ID'/>
                                                    <Value Type='Counter'>6</Value>
                                                </Eq>
                                            </Where>
                                        </Query>
                                    </View>";
                ListItemCollection listItems = list.GetItems(camlQueryForItem);
                clientContext.Load(listItems, items => items.Include
                                                (item => item["wlookup"],
                                                 item => item["Editor"],
                                                 item => item["Title"]));
                clientContext.ExecuteQuery();
                ListItem itemToUpdate = listItems[0];
                FieldLookupValue lv = itemToUpdate["wlookup"] as FieldLookupValue;
                lv.LookupId = 16;
                itemToUpdate["wlookup"] = lv;
                itemToUpdate.Update();
                clientContext.Load(itemToUpdate);
                clientContext.ExecuteQuery();
            }

    In your code snippet, you need to load the item after item. Update()  method.

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, July 6, 2015 6:10 AM
  • Hi Shoab,

    LookUpField can only update by using LookUpID which you can find from LookUPList.

    Please try to update the column using LookUpID in stead of value.

    Try This : - https://rmanimaran.wordpress.com/2012/07/16/update-lookup-field-using-client-object-model/

    regards,

    Gaurav

    Saturday, July 11, 2015 6:22 AM