4

I have an Employee list that has 2 lookup columns(Department and Manager). The below code works fine to display the employee list as a gridview, but the issue is that the these 2 lookup columns display Microsoft.SharePoint.Client.FieldLookUp in place of the actual values.

I need to display the actual values in the grid, as seen in the sharepoint list.

Kindly help. Thanks.

The following is the code using CSOM

List oList = clientContext.Web.Lists.GetByTitle("Employees");
        clientContext.Load(oList);

        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View><RowLimit>3</RowLimit></View>";

        Microsoft.SharePoint.Client.ListItemCollection itemInfo = oList.GetItems(camlQuery);
        clientContext.Load(itemInfo);
        clientContext.ExecuteQuery();


dt = new DataTable();
        DataColumn col = new DataColumn();
        col = new DataColumn("col1");
        dt.Columns.Add(col);
        col = new DataColumn("col2");
        dt.Columns.Add(col);
        col = new DataColumn("col3");
        dt.Columns.Add(col);
        col = new DataColumn("col4");
        dt.Columns.Add(col);
        col = new DataColumn("col5");
        dt.Columns.Add(col);
        col = new DataColumn("col6");
        dt.Columns.Add(col);
        col = new DataColumn("col7");
        dt.Columns.Add(col);
        col = new DataColumn("col8");
        dt.Columns.Add(col);
        foreach (var list in itemInfo)
        {
            DataRow dr = dt.NewRow();
            dr["col1"] = list["Title"];
            dr["col2"] = list["Last_x0020_Name"];
            dr["col3"] = list["u5ib"];
            dr["col4"] = list["Address"];
            dr["col5"] = list["Department"];
            dr["col6"] = list["Manager"];
            dr["col7"] = list["Gender"];
            dr["col8"] = list["Salary"];
            dt.Rows.Add(dr);
        }

The following is the code for Grid

<asp:GridView runat="server" ID="gridview" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="col1" HeaderText="First Name" />
                <asp:BoundField DataField="col2" HeaderText="Last Name" />
                <asp:BoundField DataField="col3" HeaderText="Age" />
                <asp:BoundField DataField="col4" HeaderText="Address" />
                <asp:BoundField DataField="col5" HeaderText="Department" />
                <asp:BoundField DataField="col6" HeaderText="Manager" />
                <asp:BoundField DataField="col7" HeaderText="Gender" />
                <asp:BoundField DataField="col8" HeaderText="Salary" />
            </Columns>
        </asp:GridView>
1

For lookup columns we should get values like as bellow:

// Manger column
    FieldLookupValue lookup = list["Manager"] as FieldLookupValue;
     dr["col6"] = lookup.LookupValue; // for display value; Use lookup.LookupId for lookup ID

// Department column
    FieldLookupValue lookup = list["Department"] as FieldLookupValue;
     dr["col5"] = lookup.LookupValue; 

Hope this helps you.

| improve this answer | |
  • Also could you tell how to insert values into a list that contains loopup columns? @Nagababu – Sanjana V Aug 7 '17 at 6:40
1

This is a better solution :

            //Department column
            FieldLookupValue deptItem = new FieldLookupValue();
            deptItem.LookupId = Convert.ToInt32(DropDownListDepartment.SelectedValue);
            myItem["Department"] = deptItem;
            myItem.Update();

            //Manager column
            FieldLookupValue managerItem = new FieldLookupValue();
            managerItem.LookupId = Convert.ToInt32(DropDownListManager.SelectedValue);
            myItem["Manager"] = managerItem;
            myItem.Update();
| improve this answer | |
0

For lookup columns we should set values like as bellow:

//Department column
 FieldLookupValue lv = new FieldLookupValue();
 lv.LookupId = 1; // here instead of 1, you should provide corresponding Department list item ID
 item["col5"] = lv;
 item.Update();

Hope this helps you.

| improve this answer | |
  • Adding the following lines worked myItem["Department"] = 1; myItem["Manager"] = 1; I also had to set the parent list as ID (earlier it was only title) – Sanjana V Aug 7 '17 at 7:47

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.